Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

printing array to document

function mult(a,b) { var i ; var text = " ";

for (i = 0; i < a.length; i++) {
    text = text + a[i]*b + "<br>";
}

alert( text);

}mult([5,6,7,3],6);

what exactly is the use of the text var with the white space? when i remove the text variable from the for loop, the product of the last index of the array and 6 is showing"(18)", but not the rest of the numbers in the array.

what is the text variable doing to print out each number individually? (30,36,42,18)

it may be an empty string, may be to avoid NullPointerException. correct me if I'm wrong :-)

1 Answer

text variable acting like a holder, it holds each values from the array iteration.

In each array iteration, product is calculated and value is concatenated to text variable. So that once array iteration is over text variable will have final output like this "30<br>36<br>42<br>18<br>"

But If you uninitialise text for example like this var text; output is going to be "NaN<br>36<br>42<br>18<br>". Here The NaN property represents "Not-a-Number" value.