Problem overview
The method parity receives a non-negative integer n and returns the string "even" if n is divisible by 2 and "odd" otherwise. For n = 12 the answer is "even", because 12 divided by 2 leaves nothing over.
Approach
Divisibility is a question about remainders. The modulo operator % gives the remainder of a division: 12 % 2 is 0 and 13 % 2 is 1. A number is even exactly when its remainder after dividing by 2 is 0, and odd otherwise. Since the remainder by 2 can only be 0 or 1, there is nothing else to check.
That gives a two-way choice: if the remainder is 0, return "even", and in every other case return "odd".
Step by step
- Test for even. Write an
ifwhose condition comparesn % 2with 0. The lesson uses===, strict equality, which never converts types. Here both sides are numbers, so it behaves like==, and===is the habit worth keeping. - Return
"even"inside theif. The method ends there for even numbers. - Return
"odd"after theif. This line is reached only when the condition was false, because an even number has already returned. That is why noelseis needed.
The solution in TypeScript
class Solution {
parity(n: number): string {
if (n % 2 === 0) {
return "even";
}
return "odd";
}
}
Some TypeScript details about this code:
- The test is against 0, not 1.
n % 2 === 0identifies even numbers, and everything else is odd. Testing for odd withn % 2 === 1works for the non-negative inputs of this lesson, but it breaks for negative numbers. In JavaScript and TypeScript, the result of%takes the sign of the left operand, so-3 % 2is-1, and a test against 1 would call -3 even. - The types.
n: numberand: stringare annotations for the compiler. They are erased when the code is compiled to JavaScript, so they check nothing at run time. Calling the compiled method with a string would not be stopped by them. - Numbers can be fractional. TypeScript has a single
numbertype, and the compiler accepts7.5where an integer is expected. For that input,7.5 % 2is1.5, which is not 0, so the method returns"odd". The lesson only promises correct answers for integers. - Zero is even.
0 % 2is 0, soparity(0)returns"even", which matches the mathematical definition.
A shorter form
The same logic fits in one expression with the conditional operator: return n % 2 === 0 ? "even" : "odd";. It returns the first string when the condition is true and the second when it is false. The lesson uses the if form because it shows each step separately, and the conditional operator is worth knowing once you are comfortable reading that form.
To trace n = 13: 13 % 2 is 1, which is not 0, so the if block is skipped and the method reaches return "odd". To trace n = 12: 12 % 2 is 0, the condition is true, and the method returns "even" without ever reaching the last line.
Complexity
The method does one remainder operation and one comparison, whatever the size of n, so time is O(1). It stores nothing, so space is O(1).
Edge cases and common mistakes
- Zero. It is even, and the code handles it without a special case.
- Using
/instead of%. TypeScript's division does not discard the fraction:13 / 2is6.5, never 6, so comparing a division with 0 does not test divisibility. - Using
=instead of===. A single=assigns, andn % 2 = 0is a compile error because the left side cannot be assigned to. - Returning the wrong case or spelling. The strings must be exactly
"even"and"odd", in lower case. - Testing for odd with
=== 1. This fails for negative inputs, as shown above, so prefer the test against 0.
Testing
const solution = new Solution();
console.log(solution.parity(12));
console.log(solution.parity(13));
console.log(solution.parity(0));
console.log(solution.parity(1));
even
odd
even
odd