简体   繁体   中英

Program to check if number is odd or even using switch statement

I'm trying to write a program that uses a switch statement to check if a number is odd or even.

For some reason I'm not getting anything printed to the console. When I add a default case though, it automatically prints the default (console.log("Invalid input"). Can someone explain why this approach doesn't work?

var enteredNumber = 40;
var enteredNumber_div_by2 = enteredNumber/2;

switch(enteredNumber_div_by2) {
    case Number.isInteger(enteredNumber_div_by2) === true:
        console.log(enteredNumber.toString() + " is an even number.");
        break;
    case Number.isInteger(enteredNumber_div_by2) === false:
        console.log(enteredNumber.toString() + " is an odd number.");
        break;
    case enteredNumber_div_by2.isNaN() === true:
        console.log("Invalid input");
        break;
    default: 
        console.log("Invalid input");
}

edit: In case anyone else is trying to understand switch statements like I am, I revised my code based on Rocket's answer and it works now. I realize this is not the best way to check if a number is odd or even but I'm doing this solely to understand switch statements. Also, I realized my code doesn't really check if the input is a number so even if you input "hi" it will return "hi is an odd number". Here is my revised switch statement:

switch(Number.isInteger(enteredNumber_div_by2) === true) {
    case true:
        console.log(enteredNumber.toString() + " is an even number.");
        break;
    case false:
        console.log(enteredNumber.toString() + " is an odd number.");
        break;
    default:
        console.log("Invalid input")

edit 2: Nina's answer works the best:

switch (enteredNumber % 2) {
    case 0:
        console.log(enteredNumber.toString() + " is an even number.");
        break;
    case 1:
        console.log(enteredNumber.toString() + " is an odd number.");
        break;
    default:
        console.log("Invalid input")
}

You could take the remainder and check the value of it.

The switch statement takes two values and uses a strict comparison, like === .

If you use inside case a boolean value, you need to use in the switch part another corresounding boolean value.

Even if Javascript offers to use dynamic content in both parts, some people tend to accept only constant values to compare instead of dynamic values.

 var enteredNumber = 40; switch (enteredNumber % 2) { case 0: console.log(enteredNumber + " is an even number."); break; case 1: console.log(enteredNumber + " is an odd number."); break; default: console.log("Invalid input"); }

This is is not how switch statements work. A switch is used to compare a variable to a list of values. Such as:

let a = 'a';
switch(a) {
    case 'a':
        break;
    case 'b':
        break;
}

If you want to check Number.isInteger(enteredNumber_div_by2) , then just use an if / else :

if (Number.isInteger(enteredNumber_div_by2)) {
    console.log(enteredNumber.toString() + " is an even number.");
}
else {
    console.log(enteredNumber.toString() + " is an odd number.");
}

If for whatever reason you really wanted to use a switch here, you'd have to do:

switch (Number.isInteger(enteredNumber_div_by2)) {
    case true:
        console.log(enteredNumber.toString() + " is an even number.");
        break;
    case false:
        console.log(enteredNumber.toString() + " is an odd number.");
        break;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM