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

Nick Casey
Nick Casey
1,840 Points

I'm not really sure what's being asked of me here

I've tried creating a while loop with the counter set to 26 time to run through the array. Can I get a hand here? Thanks

script.js
var count = 0;

while (count <27) {
  var randNum = randomNumber(6);
  document.write(randNum + ' ');
  count +=1;

}

5 Answers

Tomasz Budny
Tomasz Budny
14,588 Points

You are close but, you are making a call to a undefined function randomNumber() and the code in your loop is being executed 27 times.

In javascript to generate a pseudo random number, use the random() function. If you want to get a number between 1 and 10 for example, do something like this:

Math.floor((Math.random() * 10) + 1);

James Sedghi
James Sedghi
21,549 Points

Hi Nick,

You're some way there, however there are some things to improve here.

To begin with, count is declared at 0 at the top of the page. As your while loop is set to be less than 27, you're going to be running the code 27 times instead of 26. So the loop should appear like this:

var count = 0;

while (count < 26) {
  count += 1;
}

Next, you're declaring a variable called randNum that's equal to the function randomNumber(). However, there's no randomNumber function in your script, so this is causing an error.

For this task, you should be able to just print the count variable every time it loops, like so:

var count = 0;

while (count < 26) {
  document.write(count);
  count += 1;
}

In essence, all you're doing is declaring count at the beginning as 0. on every iteration of the loop you'll be printing the value of count, and then increasing it by 1.

Hope this helps.

Karolin Rafalski
Karolin Rafalski
11,368 Points

Your loop will execute 27 times. If you start from 0, then you need to have your upper limit be less than 26. If you start at 1 then you need to have your upper limit be less than 27.

Nick Casey
Nick Casey
1,840 Points

Not really sure why I'm unable to vote your answer up, @James Sedghi, but I used your instructions, and the problem worked out like a charm. I really appreciate the help everyone.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

You can't vote it up because he added it as a comment :( A mod needs to come in and hopefully change it to an "answer".

Nick Casey
Nick Casey
1,840 Points

ah, got it. Thanks, Jennifer. :-)