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 Making Decisions in Your Code with Conditional Statements Boolean Values

mattbeasley
mattbeasley
4,758 Points

About The String To Number Comparison

Just want to double check that the code snippet

if ( +guess === number ) {
   correctGuess = true;
}

is only need to make sure the program understands that we want it to treat the string as a number, I took some time to write a little code game in addition to this one and found that since I wasn't using numbers and strings I could move forward without that line.

I guess at a simpler level I am just asking, if we weren't using numbers for answers would that line not be needed? I know I was able to use the code below to create a page that displays whether or not the answer is correct or wrong. Thanks for your replies in advance.

const guess = prompt('What Is The Slow Moving Animal?');
const correct = ("<p>Good Job You Guessed The Correct Answer!</p>");
const incorrect = ("<p>Sorry That Is The Wrong Answer! The Answer Was Turtle!</p>");
if ( guess.toUpperCase() === 'TURTLE' ) {
    document.querySelector('main').innerHTML = correct;
} else {
    document.querySelector('main').innerHTML = incorrect;
}

1 Answer

Torben Korb
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree seal-36
Torben Korb
Front End Web Development Techdegree Graduate 88,520 Points

Hi codejunky01,

yes you are absolutely right. The value collected from a prompt is always a string. And if you like to compare that to a number (for example for a number game) you would need to convert your (string) input to a number. The unary operator is just one way to do so.

In your case you are looking for a special string ('TURTLE') which means you can completely ignore the conversion to a string because the input will always be this type.

See notes on MDN docs for window.prompt() method: https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt#notes

Hope this helps you understand. Happy coding!