Skip to content

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.

License

Notifications You must be signed in to change notification settings

teamkooestscholar/002JS-check-for-even-or-odd

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

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.

About

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.

Topics

Resources

License

Stars

Watchers

Forks