JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops Complete the Loop

phillip smith
phillip smith
1,146 Points

whats the answer and why

whats the answer and why

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

1 Answer

Steven Parker
Steven Parker
182,024 Points

A "while" needs a conditional expression between the parentheses, and the one here has nothing but a space there.

To make it work correctly, you need a comparison using "counter" that will be true until the loop body has run 10 times.

phillip smith
phillip smith
1,146 Points

Thank you for responding. So what would the counter comparison be?

Steven Parker
Steven Parker
182,024 Points

A typical expression would be "counter < some_value" — I'll leave it up to you to figure out what "some_value" should be to make the loop run 10 times. :wink: