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 trialandyphan
3,082 PointsHelp
Why does the answer from my prompt get compared with a number when it's not supposed to, as the video said?
var randomNumber = Math.floor(Math.random() * 6) + 1;
var guess1 = prompt("Guesser 1 I am guessing a number from 1 through 6. What is my number?");
var guess2 = prompt("Guesser 2 I am guessing the same number from 1 through 6. What is my number?");
if (Math.abs(randomNumber - guess1) < Math.abs(randomNumber - guess2)) {
document.write("Guesser 1 wins! The number was " + randomNumber + ".");
} else if (Math.abs(randomNumber - guess1) > Math.abs(randomNumber - guess2)) {
document.write("Guesser 2 wins! The number was " + randomNumber + ".");
}
else if (Math.abs(randomNumber - guess1) === Math.abs(randomNumber - guess2)) {
document.write("Tie! The number was " + randomNumber + ".");
}
1 Answer
Eric Trego
15,308 Pointsi am not completely sure what you are asking. However you have to change the prompt input from a string to a number. Remember that user input initially is a string that needs to be changed to a number to do math on it. You can use the
parseInt()
method for whole numbers and
parseFloat()
``` for decimal numbers. hope that helps and happy coding