settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: Check if a number is odd or even in JavaScript

+3 votes

How to write a JS function to check if a number is odd or even or invalid? (fractions are neither odd nor even)

The input comes as a single number argument.

Examples:

Input: 5

Output: odd


Input: 8

Output: even


Input: 1.5

Output: invalid

The output should be printed to the console.

Print odd for odd numbers, even for even number and invalid for numbers that contain decimal fractions.

asked in JavaScript category by user paulcabalit

1 Answer

+2 votes

My JS function:

function evenOdd(number) {
    let reminder = number % 2;
    if (reminder == 0) {
        console.log("even");
    } else if (reminder == Math.round(reminder)) {
        console.log("odd");
    } else {
        console.log("invalid");
    }
}

evenOdd(0);

 

answered by user richard8502
...