4

I have created this function

function num(){
    var x = prompt("please enter your first number");
    var y = prompt("please enter your second number");

    if (isNaN(x)){
        num();}
    else if (isNaN(y)){
        num();}
    else if (x>y){console.log(x + " is greater than " + y);}
    else if (y>x){console.log(y + " is greater than " + x);}
    else {console.log("the two numbers are equal");}
}

The function will only log "the two numbers are equal" as it won't run as it should, when I take off the isNaN the function runs properly with the assumption that the entered values would be numbers

Adam
  • 119
  • 1
  • 8
Mohamed Hegazy
  • 285
  • 3
  • 18

4 Answers4

0

Something like this should be what you need. Hope it helps.

var valid1 = false;
var valid2 = false;

var x = prompt("please enter your first number");
while (!valid1) {
if (isNaN(parseInt(x)))
{ x = prompt(x + " is not a number. Try Again.") }
else { valid1 = true; } }

var y = prompt("please enter your second number"); 
while (!valid2) {
if (isNaN(parseInt(y)))
{ y = prompt(y + " is not a number. Try Again.") } 
else{
valid2 = true;
if (x>y){
alert(x + " is greater than " + y); }
else if (y>x){alert(y + " is greater than " + x); }
else {alert("the two numbers are equal"); } } }

I added a validity loop to check on each prompt because I think its good practice for the end user.

I also fixed the isNaN logic which is what the OP was having trouble with and added the parseInt into it.

Kosmos
  • 101
  • 1
  • Welcome to Stack Overflow and thanks for your first answer! Please explain what you have added/changed and what the OPs failure was, this will make your answer more helpful. Thank you! – Roman Feb 25 '16 at 23:07
  • 1
    Done, let me know if that helps clarify. – Kosmos Feb 25 '16 at 23:12
0

Try this.

//function to check if input is not number
function checkIfNaN(ipNo)
{
   if(ipNo && ipNo.trim()!='') //handle empty inputs
      return isNaN(Number(ipNo)); // Number will handle decimals as well. if parseInt is used 2.1 2.9 will be treated as equal
   else 
     return true;
 }

 function TestNumber()
 {
 var num1 , num2;

 while(checkIfNaN(num1)) //request user till number is entered
 {
         num1 = prompt("please enter your first number");
  }

  while(checkIfNaN(num2))
 {
         num2 = prompt("please enter your second number");
  }

  //Compare logic
 if(num1>num2) alert("1Big");
 else if(num1<num2) alert("2BIG");
 else alert("eq");
 }

 TestNumber();

Fiddle https://jsfiddle.net/dk_dragonknight/p2wxjq8z/1/

pratikpawar
  • 2,038
  • 2
  • 16
  • 20
0

Detecting if any type of JS object or literal represents a number is a bit tricky. This is because the most of the convertion methods treat falsy values as 0, especially an empty string is converted to zero.

parseFloat and parseInt are execptional, they will return NaN from any falsy value. However, they will pass a string with trailing non-number characters. Hence we've to detect both, falsy values and strings.

The only reliable way is the method in CMS'es wiki-answer @KevinB has linked in a comment, i.e.

if (!isNaN(parseFloat(n)) && isFinite(n)) {/* Passed, we've a number */}

Then there's another problem in your code, the recursive call if invalid values were entered. If you wanted to return for example a sum of x and y, num would return a wrong value, if some invalid value(s) was/were entered before valid values. This problem won't occur in your particular code, but on the other hand, it also won't allow convertion of the variables to numbers in the correct stage either, hence you've to change the code anyway.

To fix the recursion issue, you've to check each prompt separately:

var x, y;
while (!(!isNaN(parseFloat(x)) && isFinite(x))) {
    x = prompt(...);
}
while (!(!isNaN(parseFloat(y)) && isFinite(y))) {
    y = prompt(...);
}

Now x and y are representing valid numbers, so we can convert them:

x = +x;
y = +y;

After having all this done and seen it working, we'll see, that prompt is actually an awful way to ask user for information. A better way would be a form with inputs and "Get relations" button.

Community
  • 1
  • 1
Teemu
  • 22,918
  • 7
  • 53
  • 106
-3

The isNaN function makes a good deal of semantic sense when you are trying to see if a value === NaN.

window.prompt return a a string, so you need to parse it into an integer.

var val = prompt("enter a number");
val = parseInt(val);

if (val === isNaN) {
    //do something if not a number
}
else {
    //do something else
}

You could also use a regular expression to test the input string:

var val = prompt("enter a number");
var re = /^[0-9]+$/;

var found = val.match(re);

if (found) {//parenthesis not needed for typeof
    console.log('Is a num', found);//do something if it is a number
}
else {
    console.log('Is not a num', found);//do something else
}
Ron Sims II
  • 566
  • 5
  • 10
  • sorry, but neither of those built-in functions are very helpful here. this is why folks shouldn't be so quick to delete bad answers; it stop others from re-posting the same thing later... – dandavis Feb 25 '16 at 21:21
  • NaN is such an odd bird, i forgot it that for it typeof returns 'number'. – Ron Sims II Feb 25 '16 at 21:38