Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.