Question:
Write a function isEven that checks if a passed number is even. If the given number is even, true should be returned, otherwise false.
Example: isEven(2) should return true and isEven(3) should return false.
Answer:
function isEven (n) {
return n % 2 === 0;
};
> declare a function isEven
> it has 1 parameter
> return statement is: n % 2 === 0
> % = modulo operator gives the remainder obtained by dividing two numbers
> ∴ if n is divided by 2 and the remainder is 0 then n is an even number
> we shall get true if n is even and false if n is odd by using the modulo operator and the strict equality operator
No comments:
Post a Comment