Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,657 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);
}