JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops A Closer Look at Loop Conditions

Alexander D
Alexander D
4,450 Points

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
170,478 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.

Alexander D
Alexander D
4,450 Points

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!

Steven Parker
Steven Parker
170,478 Points

Alexander De Michieli — Glad to help. You can mark a question solved by choosing a "best answer".
And happy coding!

Clayton Perszyk
MOD
Clayton Perszyk
Treehouse Moderator 45,871 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.

Alexander D
Alexander D
4,450 Points

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