JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops Create a while Loop

I am stuck on the “create a while loop” challenge

I am trying to complete the challenge to create a while loop that prints to the document 26 times. When I have tried my solution, I get this error message: “Bummer: There was an error with your code: SyntaxError: Parse error”

Here is my code:

var count = 0;
while ( count < 27) {
  document.write (count + ' '));
}

I expect that my code is missing something or there are errors that need to be fixed. Could someone please help me?

script.js
var count = 0;
while ( count < 27) {
  document.write (count + ' '));
}

1 Answer

Jennifer Nordell
STAFF
Jennifer Nordell
Treehouse Staff

Hi there! There are a couple of problems here. First, and foremost, the thing causing the parse error is the extraneous closing parentheses on your document.write(). It has one open parenthesis, but two closing parenthesis. Once you fix that, don't yet run your code. You will either get a "communication error" or the "challenge took too long to run" error. This is because you will have an infinite loop. At no point do you ever alter the value of count. You should be incrementing the value by 1 each time through the loop. Finally, the loop should run 26 times, but you start your count at 0 which means that the last number it should write out is 25. You will need to alter your conditional a bit to make this happen.

Hope this helps! :sparkles:

Hi Jennifer,

Thanks for catching my error and for all of your suggestions, which are very helpful. I was able to modify my code to the following, which ran successfully:

function randomNumber(upper) {
  return Math.floor( Math.random() * upper ) + 1;
}
var count = 0;
while ( count < 27 ) {
    var randomNum = randomNumber (6);
    document.write (count + ' ');
    count += 1;
}