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

Amber Lim
Amber Lim
4,706 Points

I keep getting the number 1 for attempts. Why?

Here's my code. I commented out what I did different from Dave.

var upper = 10000000;
var getRandomNumber = randomNumber(upper);
var guess;
var attempts = 0;

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

while ( guess !== getRandomNumber ) {
  guess = getRandomNumber; //Dave used randomNumber(upper) instead.
  attempts += 1;
}

document.write("<p>The random number was " + getRandomNumber + "</p>")
document.write("<p>It took " + attempts + " times to guess it right!</p>")

I don't understand how using randomNumber(upper) over getRandomNumber in the while loop can produce a different result. Using getRandomNumber gets me the number 1 for attempts no matter how many times I refresh it. Once I change my syntax to match Dave's, it worked.

I appreciate any help. Thank you!

3 Answers

Steven Parker
Steven Parker
216,012 Points

Your first guess is always correct, so it only takes one.

When you assign getRandomNumber at the beginning of the program, you put a random number into it. But then later, in the loop, you check that number against itself, so it will always match on the first attempt.

What the original code did was to pick a different random number each time in the loop, and try over and over again until it picked one that matched. If the range is large, that could take a while!

Amber Lim
Amber Lim
4,706 Points

Thank you! It makes sense now.

andren
andren
28,520 Points

getRandomNumber has a random number generated for it at the start that acts as the solution. By setting the guess equal to it you are essentially saying guess = solution it shouldn't be too surprising that you then get it right in one attempt. The while loop is setup to only run when the guess is not equal to getRandomNumber so the loop will exit immediately due to your code change.

The randomNumber function generates a new random number every time it is called, so by using that you are not guaranteed to get the same number that was assigned to getRandomNumber on the first attempt.

Amber Lim
Amber Lim
4,706 Points

Thanks for your answer. Helped a ton!

Julien Collet
Julien Collet
5,968 Points

As already said, the getRandomNumber variable contained the integer that was returned by the randomNumber function.

To make your code work as you wrote it, the getRandomNumber variable should contain a function that return the result of the randomNumber function (this don't seems to have much sens though, the Dave's method looks more straightforward).

So this would work :

var upper = 10000000;
var getRandomNumber = function(){return randomNumber(upper);};
var guess;
var attempts = 0;

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

while ( guess !== getRandomNumber() ) {
  guess = getRandomNumber(); //Dave used randomNumber(upper) instead.
  attempts += 1;
}

document.write("<p>The random number was " + getRandomNumber() + "</p>")
document.write("<p>It took " + attempts + " times to guess it right!</p>")