How to validate a range of number input in Javascript and end the loop with a designated input

if (F >= -100 || F <= 212) {


Consider the effect of short-circuiting in an OR expression. To set this as a range we would use && (AND) which requires both operands to be truthy.

isNAN()  =>  invalid

isNaN()  =>  valid

Again, the conditions don’t jive. This time we do want short-circuiting, so,

if (isNaN(F) || F < -100 || F > 212) {


If any one of those is truthy, the outcome will be true.

I am just really confused, I am failry new to javascript and none of this unfortunately makes sense to me. I am determined to learn

The term short-circuiting refers to when a truthy outcome occurs in an earlier operand. OR will stop evaluating on the first true.

if (isNaN() ...) {
    // code block

The condition is Not a Number being true will end the evaluation and execute the code block. The other two operands don’t even get looked at.

When F is a number, that expression will be falsy and JS will evaluate the second operand…

if (isNaN(F) || F < -100 ...) {


Again, if that condition is truthy, the final operand will be ignored, as above. If not, then the final operand is evaluated. When all three are false, the code block does not execute.

1 Like

OK, my assignment asks for the loop to run while validating the range -100 and 212, but also receiving the input of 999 in the temperature slot to stop the loop and calculate the multiple entries

I agree with @mtf that
(F >= -100 || F <= 212)
should be
(F >= -100 && F <= 212)
otherwise what’s in that if-block will run for stuff outside of the range -100 to 212

you also probably want have again != 999 in the condition for the do-while loop so that the loop does not run again if the input was 999.

There’s a better way to do that, but I don’t want to give the answer away just yet.

ok, thank you @janbazant1107978602 I am going to give it a try

Now it seems to only receive one input and gives the results. It does not continue looping for multiple entries :thinking:

here is the set of instructions

  1. Add data validation to the application so it won’t do the conversion until the user enters a Fahrenheit temperature between -100 and 212. If the entry is invalid, a dialog box like the one above should be displayed.
  2. Add a loop to the code so the user can do a series of calculations without restarting the application. To end the application, the user must enter 999 as the temperature.

upon the statement being invalid, there should be a dialogue that displays “you entered (number) enter between the range of -100 and 212”

999 is the exit control value. As long as that value is not entered, in range or not, the loop will continue.

let f;
while (f !== 999) {
  // keep entering new temperature values for `f`

One way to have it exit the loop when the input is 999 is to use break;
as in

if (F == 999) {

which would be best before the other if-statements in the loop.

here’s an adaption of your code for a Codebyte …
Try not to look at the code below until you’ve attempted it yourself again,
you’ll learn more that way.

const y = 'y'; const n = 'n'; let sample_inputs = [98, y, 212, y, -40, y, 300, y, 999, y, 100, 999]; let index = 0; function prompt(output) { console.log(output); let inputted = sample_inputs[index]; index++; console.log(" sample user input >>> " + inputted + '\n'); return inputted; } let again = "y"; do { const F = parseInt(prompt("Enter Temperature in Fahrenheit")); // get fahrenheit temperature from user if (F == 999) { break; } if (F >= -100 && F <= 212) { let C = (F-32) * (5/9); console.log(` ${F.toFixed(1)} F = ${C.toFixed(1)} C \n`); } else { console.log("Entry must range from -100 to +212 \n"); } again = prompt("continue converting? to continue, enter 'y'"); } while (again == "y" && again != 999); console.log("\n program ended");

note that again != 999 actually has no effect in the code above
(because again would have to be y in order for the loop to run again anyway.)

If you’d rather not use y, the ending could look like this instead:

  again = prompt("continue converting? to stop, enter '999'");
while (again != 999);

so that whether the value of again is 999 determines whether you exit the do-while loop or not.

Thank you very much for all your help @mtf and @janbazant1107978602!

I wasn’t defining F at the beginning, as mentioned above, that is what seemed to be the problem. Upon defining F, the loop was able to continue until inputting 999.

1 Like