Back to Interview Prep

Warm Up · Function, if-else

Even or Odd Number

Problem Statement

Write a function that accepts a number and checks whether it is Even or Odd. If the number is divisible by 2, it is an Even number; otherwise, it is an Odd number. Test the function with inputs 18 and 5.

Example

Input: 18, then 5

Process: 18 % 2 = 0 → Even; 5 % 2 = 1 → Odd

Output: Even Number, Odd Number

Approach

  • Create a function that takes a number.
  • If number % 2 === 0, it is "Even".
  • Otherwise, it is "Odd".

Visualisation

1Take the given number18
2Check the remaindernumber % 2 === 0 ?
3Yes → even"Even Number"
4No → odd"Odd Number"

Explanation

  • Accept the input number in the function.
  • Check if the number modulo 2 equals 0 - % gives the remainder after division.
  • If yes, print or return "Even".
  • Otherwise, print or return "Odd".
  • Test the function with different numbers (like 18 and 5) to verify correctness.

Your solution

Solve it in JavaScript and print the answer with console.log(). Run to see your output, then Submit to check it against the test cases.

// Write your solution in JavaScript
// Print the answer with console.log()


Console

No output yet. Use console.log() to print values here.

Write your own solution and press Submit once to unlock the answer.

You're viewing a free lesson. Sign up free to track your progress and unlock the full course.Sign up freeSign in