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 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
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

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;
}