2

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;

}

1
  • 1
    can i have some input / output sample? Commented Oct 15, 2016 at 14:15

5 Answers 5

2

This 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("&nbsp;");
        } else {
            dash.push("_"); 
        }

    }

    return dash;

}
Sign up to request clarification or add additional context in comments.

Comments

2

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("&nbsp;");
        } else {
            dash.push("_");
        }
    }
    return dash;
}

var dashedSingle = dash('Testing');
var dashedMultiple = dash('Testing this sentence now');

console.log(dashedSingle)
console.log(dashedMultiple)

Comments

2

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("&nbsp;");
            } else {
                dash.push("_"); 
            }
        }

      return dash;
    }

Comments

0

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 === ' ' ? '&nbsp;' : '_')
)

Comments

-1

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(" ")

7 Comments

Why would you need to use the character code? word[i] is a string, not a code.
every character has its ascii code. the assembler access every character with its code. you can see here smashingmagazine.com/wp-content/uploads/2012/04/…
Yes, every character has a code. But word[i] doesn't return the code, it returns the character. You would have use word.charCodeAt(i) == " ".charCodeAt(0).
You're also calling charCodeAt() wrong. It's a method of String, and the argument is the index.
it tried to answer you. if its not suitable for you then you should not mark my answer as not useful. :(
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.