evenOrOdd.js (8kyu 01)

Codewars 알고리즘 풀이


Problem

  • Create a function that takes an integer as an argument and returns ‘Even’ for even numbers or ‘Odd’ for odd numbers.
    • 짝수라면 ‘Even’을 반환하고, 홀수라면 ‘Odd’를 반환해라.


Solution 01

function evenOrOdd(n) {
  if (n % 2 === 0) {
    return 'Even';
  } else {
    return 'Odd';
  }
}

evenOrOdd(0);  // Even
evenOrOdd(1);  // Odd
evenOrOdd(2);  // Even


Solution 02

function evenOrOdd(n) {
   return n % 2 === 0 ? 'Even' : 'Odd';
}

evenOrOdd(0);  // Even
evenOrOdd(1);  // Odd
evenOrOdd(2);  // Even


Solution 03

function evenOrOdd(n) {
   return n % 2 ? 'Odd' : 'Even';
}

evenOrOdd(0);  // Even
evenOrOdd(1);  // Odd
evenOrOdd(2);  // Even


Solution 04

const evenOrOdd = n => n % 2 ? 'Odd' : 'Even';

evenOrOdd(0);  // Even
evenOrOdd(1);  // Odd
evenOrOdd(2);  // Even