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

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 91,433 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!