bob0306 bob0306 - 4 years ago 83
Javascript Question

javascript returning NaN when it is compared with numbers to non-numbers

function largerNum(num1,num2){
c = (num1>num2)?num1:num2;

return c;
}

console.log('largerNum(2095,106) returns: ' + largerNum(2095,106));
console.log('largerNum("23",14) returns: ' + largerNum("23",14));
console.log('largerNum(108,"a") returns: ' + largerNum(108,"a"));

console.log("\n");


for this code, it compares two parameters and returns the bigger number and I wanted to see how I could return NaN, when it is compared with numbers to something that is not number like (108,a)

Answer Source
function largerNum(num1,num2){
   if (isNaN(num1) || isNaN(num2)) {
      return NaN;
   }
   c = (num1>num2)?num1:num2;
   return c;
}

Hope this helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download