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 trialHannah Mackaness
4,437 Pointsthe guess and the correct answer are always equal..it's not "random"
In the tutorial the two numbers are different- it takes the computer 450 guesses to get the number 2387 but with my code it the computer just keeps adding 1 to 0 until it reaches the desired number.
So it still works, but it's different. Why?
var upper = 10000
var randomNumber = getRandomNumber(upper);
var guess;
var attempts = 0;
function getRandomNumber(upper) {
return Math.floor( Math.random() * upper ) + 1;
}
while (guess !== randomNumber) {
guess = getRandomNumber (upper);
attempts += 1;
}
document.write ("<p> the random number was: " + randomNumber + "</p>");
document.write ("<p> it took the computer: " + guess + "attempts to get it right. </p>");
4 Answers
Jonathan Grieve
Treehouse Moderator 91,253 PointsWell in programming the rand function never really gives us a truly random number. The result of the rand()
function is a response of anything between 0 and 1. You then have to put another function on it to get a whole number then multiply it by any other number to get a range.
The code looks fine to me, the attempts
variable simply increments by 1 whenever the numbers are not matching... unless there's something I've missed. :-)
Jacob Mishkin
23,118 PointsThe issue that I saw, that would make the number of guesses equal the initial guess is in your while loop the variable guess is using the same random number function as the initial guess, so both variables will receive that number. You are also not outputting the attempts made by the computer. you are outputting the guess variable. I refactored your code below. If you have any questions please ask.
var upper = 10000
var randomNumber = getRandomNumber(upper);
var guess;
var attempts = 0;
function getRandomNumber(upper) {
return Math.floor( Math.random() * upper ) + 1;
}
while (guess !== randomNumber) { //Here the var guess is guessing the number
attempts += 1; // you want to iterate through the attempts and list how many times the comp guesses
}
document.write ("<p> the random number was: " + randomNumber + "</p>");
document.write ("<p> it took the computer: " + attempts + "attempts to get it right. </p>"); // List the number of attempts
Hannah Mackaness
4,437 Pointsahhhhh hhaaaaaaaaa thank you, now I have a better understanding :)
Jacob Mishkin
23,118 PointsAwesome! Not a problem.
rocky roy
556 Pointswhen randomnumber and guess is same then why it's not matching on first time. why it's taking long time to match the random numbr.
Hannah Mackaness
4,437 PointsHannah Mackaness
4,437 PointsThanks, Your explanation of the function was really helpful- what I still don't understand is why the code seems to be the same as in the tutorial but that it runs differently. How come when it ran in the tutorial it didn't generate what I am getting. Which is always something like: The number is 476 and the computer took 476 guesses to complete the task
Did I ask the computer to start from 0 and then add one until it matched the random number it generated earlier?
Jonathan Grieve
Treehouse Moderator 91,253 PointsJonathan Grieve
Treehouse Moderator 91,253 PointsHi Hannah,
Yes I think that's exactly what's happening.
If you replace
document.write ("<p> it took the computer: " + guess + "attempts to get it right. </p>");
with
document.write ("<p> it took the computer: " + attempts + "attempts to get it right. </p>");
That should give you a more accurate set of results, because attempt's is the variable that's been incremented. Guess is simply a variable that stores a function which is a
method
/Good luck. :-)