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 trial

JavaScript JavaScript Basics (Retired) Making Decisions with Conditional Statements Boolean Values

I 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.

OHHH I got it I was confusing randomNumb with inputNumb thanks !!

2 Answers

inputNumb 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.