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

I'm a bit confused about "guess" being a string. I don't understand why it is?

When I run the code without +guess (just using guess) I get no errors, whether with correct or incorrect guesses. Since the ultimate value that gets assigned to guess is the number from the prompt, why is it a string?

2 Answers

Cameron Childres
Cameron Childres
11,818 Points

Hi Henry,

Prompt returns a string. Check out "return value" under w3schools documentation for prompt():

Return Value: A String. If the user clicks "OK", the input value is returned. If the user clicks "cancel", null is returned. If the user clicks OK without entering any text, an empty string is returned.

You can turn this value in to a number with the parseInt() function, like parseInt(guess)

Hi Cameron - In a subsequent video he went into this a bit further. I understand the concept, but when I tested that particular code in the Chrome JS Console, I didn't receive an error, which added to my confusion. But yes, when he went over the parseFloat, parseInt and unary + it started making a lot more sense.

I did start reading the w3 documentation.

Thank you!