While loop issue with Prompt (Adding numbers on Prompt)

While loop issue with Prompt (Adding numbers on Prompt)
0

#1

Adding two number with prompt input. Pretty simple. The problem is if the user clicks OK without any input or inputs a string, i want the prompt to keep repeating itself. But here, it just repeats once and then disappears.

I wrote this -

<button id='add'> Add </button>

function add() {
    var xp = prompt("First Number");
  while(xp === "" || isNaN(xp) ) {
    var xp = prompt("Please, Enter First Number");
    }
  if (xp) {
  var pt = prompt("Second Number");
    while(pt === "" || isNaN(pt) ) {
    var pt = prompt("Please, Enter Second Number");
    }
    var po = parseInt(xp);
    var co = parseInt(pt);
    var yoo = po+co;
    if (isNaN(yoo)) {
      document.write("");
    }
    else {
  document.write(yoo);
    }
    }
  }

document.getElementById('add').addEventListener("click", function(){
  add();
});

Link - JS Bin
Help !


#2

It’s Firefox.
:frowning:

Working perfectly on Chrome.


#3

Works for me on Firefox as well.


#4

I can only suggest you use better variable naming :slight_smile:


#5

I see in the console there is suggestion to add
// noprotect
as the first line of your JS file. Try it.


#6

@jenovs Thanks. Seems like it’s JS Bin that’s causing the problem in Firefox.


#7

This ^^^

I know they call it ‘coding’, but that doesn’t mean you should aim to be indecipherable! :slight_smile: