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 trialEetu Hämäläinen
1,479 PointsImprovement to the code.
Hello, I'm trying to think a correct way to how to prompt and ask the number from user and then the computer will guess it.
//This code is working but the prompt value is only the upper value.
var upper = prompt('Write a number between 1 to 10000.'); var comprandomNumber = getRandomNumber(upper); var guess; var attempts = 0;
function getRandomNumber(upper) { return Math.floor( Math.random() * upper ) + 1; }
while ( guess !== comprandomNumber ) { guess = getRandomNumber( upper ); attempts = attempts + 1; } document.write('<p>The random number was: ' + comprandomNumber + '<p>'); document.write('It took the computer ' + attempts + ' attempts to get it right.');
//I tried to make var comprandomNumber = upper; but I guess thats not right way.
2 Answers
Steven Parker
231,236 PointsIf you want the computer to guess your number, you won't need to select a random one to start with. But you will need to convert the string input from "prompt" into a number if you want to use a type-sensitive comparison ("!=="):
var upper = 10000;
var comprandomNumber = parseInt(prompt("Write a number between 1 to 10000."));
Eetu Hämäläinen
1,479 PointsThank you!