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 JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops Complete the Loop

What is the answer to this question ?!!

I am adding 1 < = 10 in the parenthesis of my while loop and it is not being accepted :( No hint, and related questions are ALL over the fourms, seriously people?!

script.js
var counter = 1;
while ( ) {
    document.write("<p>Now in loop #" + counter + "</p>");
    counter += 1;
}

oh by the way, the quiz requirement is to get the code to run 10 times

2 Answers

Steven Parker
Steven Parker
231,269 Points

The "while" needs an expression inside the parentheses to evaluate to determine whether to continue or not.

You probably want to compare "counter" with some value.

I'll bet you can get it now without an explicit code spoiler. :wink:

I know lol, thank you! I was very tired and I figured it out like 2 minutes later XD

Your code inside the parenthesis should be:

while (counter <= 10) 

As every time you run through the loop counter will be incremented by one and the condition will be checked against counter. Setting the condition as you have at the moment will cause an infinite loop as 1 will always be less than 10 and is never incremented.

If I am mistaken, please let me know.

Lol thank you! I asked the question before giving myself some time to figure it out. I got it! You are awesome! Thank you for answering my question. :)