JavaScript JavaScript Numbers Working with Numbers Convert Strings to Numbers

Kylie Soderblom
Kylie Soderblom
2,699 Points

is 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
MOD
Jason Anders
Treehouse Moderator 145,228 Points

HI Kylie Soderblom

While 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! :) :dizzy:

Kylie Soderblom
Kylie Soderblom
2,699 Points

Thank 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);