Skip to content

Latest commit

 

History

History
31 lines (20 loc) · 989 Bytes

README.md

File metadata and controls

31 lines (20 loc) · 989 Bytes

EASY: 002 JS - Check For Even or Odd

Description

Write a JavaScript function that takes an integer as an argument and returns "Even" if it's an even number or "Odd" if it's an odd number.

Sample solution

function checkEvenOrOdd(number) {
  if (number % 2 === 0) {
    return "Even";
  } else {
    return "Odd";
  }
}

Bonus Challenges

  • Modify the function to handle non-integer inputs gracefully. Return an error message if the input is not an integer. (Hint: Use typeof operator). [+5 extra credit points]

  • Implement the function without using conditional statements (if-else). (Hint: Use Math.floor and Math.abs functions). [+5 extra credit points]

  • Extend the function to check if the input is a positive or negative even or odd number. (Hint: Use Math.sign function). [+5 extra credit points]

How to answer?

  • Kindly go to src directory and edit solution.js file.

  • Replace the commented code with your solution.