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 trialKylie Soderblom
3,890 Pointsis there a way to use parseint with the prompt? If you are prompting for & expecting a number, change it once right away
ideally - var essentialNumber = parseint(prompt("Give me a number.")); Changing it once and not each time you have to use it later. otherwise you have to var once = prompt("Give me a number"); var twice = parseint(once);
1 Answer
Jason Anders
Treehouse Moderator 145,860 PointsWhile using parseint
at the prompt would work if you're expecting a number, but remember a number is only what you are "expecting".
You can't trust users to always do what is expected. Just because a number should be entered into the prompt doesn't mean a number will be entered into the prompt. The user may want out and try "exit" or "quit" ... or really type anything in there for no reasons at all.
So, it is best to have the input come in as is and then use some form of validation to ensure it is a numerical value before you use parseint
. This will save many headaches for you further down the line.
I hope that helps clarify your question.
Keep coding! :)
Kylie Soderblom
3,890 PointsKylie Soderblom
3,890 PointsThank you! So I would (skipping curly brackets and just talking it thru) var resp = prompt("Give me a number"); If resp != a number, then alert("You must give a number'); else let respNumber = parseInt(resp);