-1

So i have this simple script that populates a variable with an array. I then use a function with a for loop to iterate trough the array to get it's index values.

function printAllArrayValues(array) {
    for (var i = 0; i < array.length; i++) {
        var c;
        c += array[i];
    }
    return c;
}
var colorArray = ["brown", "blue", "green"];
alert(printAllArrayValues(colorArray));

The function returns a string containing all the array values but the first value = undefined. See fiddle: http://jsfiddle.net/vcyum/

Why is that?

4 Answers 4

1

The initial value of the c variable is undefined. The next line c += 'stuff' adds the string 'stuff' to the value in c. Since the initial value of c is undefined, it is cast to a string, resulting in 'undefined', so the value of c is now 'undefinedstuff'.

Your code can be fixed like this:

function printAllArrayValues(array) {
    var c = '';
    for (var i = 0; i < array.length; i++) {        
        c += array[i];
    }
    return c;
}
var colorArray = ["brown", "blue", "green"];
alert(printAllArrayValues(colorArray));

or simpler:

var colorArray = ["brown", "blue", "green"];
alert(colorArray.join(''));
Sign up to request clarification or add additional context in comments.

4 Comments

Damn, this is offcourse the solution... did i miss that because i'm just beginning javascript?
Yes. You will get better with experience :). Also, if you really want to see what is happening, Firebug or Chrome's Developer Tools are your best friend. At the very least console.log instead of alert can make a huge improvement until you learn to use breakpoints.
Yes, i first used firebug's script tool and added a breakpoint. But the problem wasn't a syntax error offcourse but a mis interpretation on my account. Firebug won't help me here i believe?
Sure it does. In this case you could have discovered the problem by placing a breakpoint on the c+= line and watching the c variable. First pass you would see undefined, second pass it would be 'undefinedbrown'. From that point it's not hard to see that the problem isn't with your array, but with the initial value in the c variable.
1

An simpler solution :

var colorArray = ["brown", "blue", "green"];
alert(colorArray.join('')); // "brownbluegreen"
alert(colorArray.join(',')); // "brown,blue,green"

2 Comments

Thx, but i'm learning for loops at the moment. So i'm sure your solution is the way to go but it's not relevant for me on this moment...
Indeed, I'm obviously off topic :)
0

To fix your bug, you can change

for(var i = 0; i < array.length; i++){
    var c;

to

var c = '';
for(var i = 0; i < array.length; i++){

so that you don't add to undefined (which is transformed to "undefined" in the string concatenation) at the first iteration.

But you could also replace your whole function with colorArray.join('').

Comments

0

Within your loop you declare c, but assign nothing to it. The variable thus has the 'value' undefined. Now if you add a string to it, undefined is boxed into a string and the string you add is appended to that.

Anyway: no need for a loop here: use colorArray.join(''); for the same result.

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.