oddeven("asd") output keep showing asd is odd. How to make it show asd is not a number?
let num;
function oddeven(num){
num = parseInt(num);
if(num%2 == 0){
document.write(num + " is even" + "<br>");
}else if(num%2 != 0){
document.write(num + " is odd" + "<br>")
}else
document.write(num + " is not a number" + "<br>");
}
oddeven(2);
oddeven(1);
oddeven(10);
oddeven(7);
oddeven("asd");
You can modify your function to check for non-numbers like this:
function oddeven(num) {
if (isNaN(num)) {
document.write(num + " is not a number" + "<br>");
} else {
num = parseInt(num);
if (num % 2 == 0) {
document.write(num + " is even" + "<br>");
} else {
document.write(num + " is odd" + "<br>")
}
}
}
UPDATE
problem in your code: in the case of oddeven("asd")
, your variable num
was being set as NaN
so your second test was passing.