Here's one possible approach:
var sQuantity;
do {
sQuantity = parseInt(prompt('Prompt message', 'Default prompt value'), 10);
} while (!sQuantity);
The code shown in your question is wrong by two reasons. First, you don't actually check for sQuantity
being equal to 0 - you assign 0 to it (with = 0
; should've been == 0
at least). To be precise, actually assigned is the result of 0 || isNaN(sQuantity)
. And it's false
- as sQuantity
is equal to 0 at the moment of assignment, not NaN
.
But even more important is the second flaw: there's no sense checking for sQuantity
value with if
right after you assigned a some value to it. You do know that it's equal to 0, right?
The only case when it makes sense is when you use a loop: first assigning some value to the variable (as result of prompt
), then checking this value in the loop's condition.
In my example I've used do-while
loop, as you obviously have to show prompt
to a user at least once. With !sQuantity
clause I check for both NaN
and 0
: these are the only falsy values that could be returned by parseInt
.