Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Even or Odd

Fundamentals: Part 1easy

Course · Section 2: JavaScript Fundamentals – Part 1 · Lecture 11: Data Types

Implement `parity(n)` returning the string `even` or `odd` for an integer `n`.

Sample tests

Input: parity(4)
Output: "even"
Input: parity(7)
Output: "odd"

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • For negative numbers, % keeps the sign, but parity still works since 0 stays 0.

Learning resources

  • MDN: typeof
Visualize this concept: Operator Precedence and Associativity →
Approach & explanation (try first)

The remainder of division by two tells you parity: zero means even, otherwise odd.

Loading...
⌘/Ctrl + Enter

Run your code to see results.