admin管理员组文章数量:1355559
I am trying to make a hangman game. So I have a function that takes the word and makes a new array of the underscore dashes. I have that working perfectly but now I am trying to add the functionality of have spacing so multiply words. But now it adds random spaces instead.
Any Help?
function dash(word) {
var dash = [];
for (var i = word.length - 1; i >= 0; i--) {
if (word[i] == " ") {
dash.push(" ");
} else {
dash.push("_");
}
}
return dash;
}
I am trying to make a hangman game. So I have a function that takes the word and makes a new array of the underscore dashes. I have that working perfectly but now I am trying to add the functionality of have spacing so multiply words. But now it adds random spaces instead.
Any Help?
function dash(word) {
var dash = [];
for (var i = word.length - 1; i >= 0; i--) {
if (word[i] == " ") {
dash.push(" ");
} else {
dash.push("_");
}
}
return dash;
}
Share
Improve this question
asked Oct 15, 2016 at 14:08
Justin BestemanJustin Besteman
3982 gold badges6 silver badges18 bronze badges
1
- 1 can i have some input / output sample? – Alongkorn Commented Oct 15, 2016 at 14:15
5 Answers
Reset to default 2This spaces aren't random – they inverted.
It's because of you running your word from back to front:
instead of for (var i = word.length - 1; i >= 0; i--)
try it:
function dash(word) {
var dash = [];
for (var i = 0; i < word.length; i++) {
if (word[i] == " ") {
dash.push(" ");
} else {
dash.push("_");
}
}
return dash;
}
It's not that it's adding random spaces, it's just backwards because you're traversing the string backwards. You're starting from the end and working your way to the front of it. It works with strings consisting of a single word because you always have only that amount of spaces.
Just change
for (var i = word.length - 1; i >= 0; i--)
to
for (var i = 0; i < word.length; i++)
Here's something to run in your console to test:
function dash(word) {
var dash = [];
for (var i = 0; i < word.length; i++) {
if (word[i] == " ") {
dash.push(" ");
} else {
dash.push("_");
}
}
return dash;
}
var dashedSingle = dash('Testing');
var dashedMultiple = dash('Testing this sentence now');
console.log(dashedSingle)
console.log(dashedMultiple)
You are traversing the elements in reverse order in for loop.... i tried this and worked fine...
function dash(word) {
var dash = [];
for (var i = 0; i <=word.length - 1; i++) {
if (word[i] == " ") {
dash.push(" ");
} else {
dash.push("_");
}
}
return dash;
}
If you just want your word in an array you could use String.prototype.split
to do the job for you. Then map over the character array.
var word = 'javascript is awesome'
console.log(
word.split('').map(letter => letter === ' ' ? ' ' : '_')
)
you should use ASCII value of space instead of word[i] == " "
im not sure about ASCII value of space in javascript. but dont worry you can use charCodeAt();
word[i] == charCodeAt(" ")
本文标签: Adding space in an array in JavaScriptStack Overflow
版权声明:本文标题:Adding space in an array in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743975877a2570875.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论