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 Numbers The Math Object Random Number Challenge – Two Numbers Solution

Osas Oyara
Osas Oyara
2,355 Points

Random Number

I am having issues getting this block of code to enter the first portion of the conditional statement. Even when entering 2 if runs the else portion.

let userGuess = prompt('Please provide a number?'); //Collect input from a user if(userGuess === 2){ userGuess =+ +userGuess; // convert input to a number userGuess =+ Math.random() + userGuess; //Use Math.random() and the user's number to generate a random number console.log(userGuess); }else{ console.log('You did not provide a number'); console.log(userGuess); console.log(typeof userGuess); }

1 Answer

Steven Parker
Steven Parker
231,275 Points

The "prompt" function always returns a string, and because of the type-sensitive comparison (===) the input can never match the number 2.

An easy fix would be to compare with the string "2" instead, or use the normal comparison operator (==).