[해결로 표시] JavaScript에서 숫자가 홀수인지 짝수인지 확인합니다.

  

3
주제 스타터

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.

예시:

입력: 5

출력: odd


입력: 8

출력: even


입력: 1.5

출력: 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.

1개 답글
2

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);
공유: