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 A Closer Look at Loop Conditions

Help with a Loop!

Hello there,

I am playing with a LOOP. Personal note, I didn't know they could crash your browser. That is a killer weapon!

I am trying to store two random numbers into two variables, and compare them until they match so the loop is broken. Something is missing though.

This is the start. I've tested this and it should work:

var upper = 20;
var attempts = 0;
var random = getRandomNumber(5);

function getRandomNumber(upper) {
  return Math.floor( Math.random() * upper ) + 1;
}

while (random !== getRandomNumber(upper) ) {
     document.write("This is the first random number " + random + " and this is the second " + getRandomNumber(upper)+ " random number <br>")
     ;
    attempts = attempts + 1;
}

document.write("<p> The random number was " + random + "</p>");
document.write("<p> It took the computer " + attempts + " attempts to guess it</p>");

Now, I do not know what's the next step. I tried creating another variable that pulls content from the same function, named "var TwoRandom = getRandomNumber(15)", but it doesn't work.

I also duplicated the function, named it differently, and created another variable to store its result. It doesn't work.

The idea that I have in mind would be something like that. If you play it, it goes into an infinite loop, so I do recommend tweaking this before.

var attempts = 0;
var random = getRandomNumber(5);
var TwoRandom = getRandomNumber(15);


function getRandomNumber(upper) {
  return Math.floor( Math.random() * upper ) + 1;
}


while (random !== TwoRandom ) {
     document.write("This is the first random number " + random + " and this is the second " + Tworandom + "  random number <br>")
     ;
    attempts = attempts + 1;
}

document.write("<p> The random number was " + random + "</p>");
document.write("<p> It took the computer " + attempts + " attempts to guess it</p>");

Any ideas on why this is not working out?

2 Answers

Steven Parker
Steven Parker
210,850 Points

In the second example, both random numbers are chosen before the loop starts. Since neither one is changed during the loop, the loop continues indefinitely. You probably want to pick the 2nd number inside the loop.

In the first example, a new number is picked for each loop, but not saved. The message picks another random number so the two numbers printed are not likely to be the same.

Hello Steven,

Thanks for your response!

It absolutely makes sense! If the numbers never change, they will never match and the loop runs for ever. Thanks!

Clayton Perszyk
MOD
Clayton Perszyk
Treehouse Moderator 46,860 Points

I just tested the first in jsbin and it works:

In the second example, which is preferred, you need to update the value of TwoRandom in the loop. Also, in the string concatenation, you are using Tworandom, not TwoRandom.

Thanks for taking the time to respond to my question and for testing the program. I really appreciate it!