Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops What are Loops?

Prakhar Patwa
Prakhar Patwa
11,260 Points

Regarding while loop

function randomNumber(upper) {
  return Math.floor( Math.random() * upper ) + 1;
var counter = 0;
var number = prompt("enter the number");
    document.write("you entered the string");
while(counter < 10){
    var randNum = randomNumber(number) ;
    document.write(randNum + " ");

it showing the o/p 1 1 1 1 1 1 1....... i am not getting the solution

Chris Higgins
Chris Higgins
6,813 Points

I put your code in my workspace as i dident really see anything wrong with it and it seem to do what you want it to do. Displays 10 numbers up to and and including the number you posted in the prompt.

It has been a while since I did this, but your "while" statement is incorrect. The proper format from MDN is:

var i = 0; do { i += 1; console.log(i); } while (i < 5);

Assuming the rest of the code is correct, you should change it as follows to make a properly formatted Do While loop.

} else { do { var randNum = randomNumber(number); document.write(randNum + " "); counter++; while (counter < 10);

These code challenges are very picky. You wrote "you entered the string". I suspect the directions were to write "you entered a string". The difference between the string and a string can make you fail the code challenges even though the code is still correct.


Ted Sumner, a do ... while loop is different (but similar) from the while loop in JavaScript. The do ... while loop runs the code before checking the conditional, the while loop checks the conditional first.

1 Answer

Did you manage to solve this problem? I was wondering if the reason it didn't work was because even if the user enters a number in the prompt it is recorded as a string. Perhaps you needed to use parseInt to covert the prompt to an integer.

Nevertheless, I ran your code and it worked just fine. I think it is possible that the random numbers could have just been what you saw, though an admittedly rare occurence.