Javascript function, if else, isNaN -


i need figure out i'm doing wrong, i've tried make function you're supposed enter 2 numbers in 2 different boxes , loop until put in valid number! (sorry code , english)

var x = parseint(prompt("please enter number!")); var y = parseint(prompt("please enter number!"));  function add(x, y) {      var z = x + y;     var = false;      {         if (isnan(x)) {             alert("invalid entry. please enter number!")         } else if (isnan(y)) {             alert("invalid entry. please enter number!")         } else {              alert(x + " + " + y + " = ");             = true;         }         while (i == false);     } }  add(x, y); 

there couple of problems code:

  1. your while misplaced.
  2. the parameters x , y don't make sense, because user needs input them.
  3. the prompts asking numbers outside of loop.

here fixed code:

function add() {      {         var x = parseint(prompt("please enter number!"));         var y = parseint(prompt("please enter number!"));         var z = x + y;         var = false;          if (isnan(x)) {             alert("invalid entry. please enter number!")         } else if (isnan(y)) {             alert("invalid entry. please enter number!")         } else {              alert(x + " + " + y + " = " + z);             = true;         }     }     while (i == false); }  add(); 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -