simpleMultiplication.js (8kyu 47)

Codewars 알고리즘 풀이


Problem

  • This kata is about multiplying a given number by eight if it is an even number and by nine otherwise.
    • 주어진 숫자가 짝수라면 8을 곱하고, 홀수라면 9를 곱한다.


Solution 01

function simpleMultiplication(n) {
  if (n % 2 === 0) {
    return n * 8;
  } else {
    return n * 9;
  }
}

simpleMultiplication(1);  // 9
simpleMultiplication(2);  // 16
simpleMultiplication(3);  // 27
simpleMultiplication(4);  // 32


Solution 02

function simpleMultiplication(n) {
  if (n % 2 === 0) return n * 8;
  if (n % 2 === 1) return n * 9;
}

simpleMultiplication(1);  // 9
simpleMultiplication(2);  // 16
simpleMultiplication(3);  // 27
simpleMultiplication(4);  // 32


Solution 03

function simpleMultiplication(n) {
  return n % 2 === 0 ? n * 8 : n * 9;
}

simpleMultiplication(1);  // 9
simpleMultiplication(2);  // 16
simpleMultiplication(3);  // 27
simpleMultiplication(4);  // 32


Solution 04

function simpleMultiplication(n) {
  return n % 2 ? n * 9 : n * 8;
}

simpleMultiplication(1);  // 9
simpleMultiplication(2);  // 16
simpleMultiplication(3);  // 27
simpleMultiplication(4);  // 32


Solution 05

function simpleMultiplication(n) {
  return n * (n % 2 ? 9 : 8);
}

simpleMultiplication(1);  // 9
simpleMultiplication(2);  // 16
simpleMultiplication(3);  // 27
simpleMultiplication(4);  // 32