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 trialangiejang
3,134 PointsI don't know what's wrong with my code
<script>
var correctGuess = false;
var randomNumb = Math.floor(Math.random()*6)+1;
var randonNumb = parseInt(randonNumb);
var inputNumb = prompt("Can you type random number?");
if (inputNumb === randomNumb) {
correctGuess = true;
}
if (correctGuess) {
document.write('You got it!')
} else {
document.write ('Sorry. The number was ' + randomNumb)
}
/* or */
var correctGuess = false;
var randomNumb = Math.floor(Math.random()*6)+1;
var inputNumb = prompt("Can you type random number?");
if (parseInt(inputNumb) === randomNumb) {
correctGuess = true;
}
if (correctGuess) {
document.write('You got it!')
} else {
document.write ('Sorry. The number was ' + randomNumb)
}
</script>
All I did differently is that I didn't put pareInt inside a conditional statement when I did put pareInt inside conditional statement it worked I just don't understand what is the difference.
2 Answers
Jasper Maposa
26,918 PointsinputNumb is of type string whereas randomNumb is an integer, therefore without parseInt applied to inputNumb that deep equality conditional statement will never be true. Remember for it to be true, both value and type must be the same. Also, every user input collected through prompt
is always a string.
Corey Miller
Full Stack JavaScript Techdegree Graduate 17,460 PointsYou might also want to add <p> tags or heading tags on your document.write lines, so you can format the text.
angiejang
3,134 Pointsangiejang
3,134 PointsOHHH I got it I was confusing randomNumb with inputNumb thanks !!