0

Why does the following code not work? Can't figure it out.

            var string = "";
            for (var x; x < numbersArray.length; x++)
                string += numbersArray[x];
            alert(string);

string is empty at the end.

3
  • 2
    and what is the value of x at the starting ? Commented Sep 28, 2014 at 2:04
  • Did you try a debugger? Commented Sep 28, 2014 at 2:04
  • 4
    Also having variable names like "string" is considered bad practice. Btw. you could also use numbersArray.join(""). Commented Sep 28, 2014 at 2:06

2 Answers 2

4

x is undefined, which is not less than any number. Therefore, your terminating condition is always false.

You probably want to start at 0.

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

Comments

2
        var string = "";
        for (var x=0; x < numbersArray.length; x++)
            string += numbersArray[x];
        console.log(string);

Just make sure to initialize your x.

Comments

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.