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 Introduction to Programming Control Structures Loops

Sukhraj Grewal
Sukhraj Grewal
10,554 Points

Code doesn't do what i expected it to do

for ( var counter = 5; counter; counter = counter -1) {
  alert("Hello WOrld", counter);
}

This suppose to alert Hello world with counter at the end 10 times and every time counter gets - 1 .

But after i run this code this alerts hello world 10 times but doesn't display the counter

Any Help ?

3 Answers

Sean T. Unwin
Sean T. Unwin
28,690 Points

alert() takes only 1 argument. If you want to see counter displayed use the following format:

alert("Hello World " + counter);

console.log(), which is used in the video, does take comma separated objects to be output:

console.log("Hello World", counter);
Bryan Jenkins
Bryan Jenkins
5,438 Points

Try this out. Javascript uses the + symbol to concatenate a string.

for (var counter=5; counter; counter--) {
  alert("Hello World " + counter);
}