Algorithm/codewars

Calculating with Functions 답안

Bonita SY 2021. 3. 24. 18:32
728x90
반응형

문제)

www.codewars.com/kata/525f3eda17c7cd9f9e000b39/train/javascript

 

Codewars: Achieve mastery through challenge

Codewars is where developers achieve code mastery through challenge. Train on kata in the dojo and reach your highest potential.

www.codewars.com

 

내가 푼 답)

function zero(func = null) {
  return calculate(0, func);
}
function one(func = null) {
  return calculate(1, func);
}
function two(func = null) {
  return calculate(2, func);
}
function three(func = null) {
  return calculate(3, func);
}
function four(func = null) {
  return calculate(4, func);
}
function five(func = null) {
  return calculate(5, func);
}
function six(func = null) {
  return calculate(6, func);
}
function seven(func = null) {
  return calculate(7, func);
}
function eight(func = null) {
  return calculate(8, func);
}
function nine(func = null) {
  return calculate(9, func);
}

function calculate(num, func) {
  if (func === null) {
   return num; 
  } else {
    switch(func.operator) {
      case '+':
        return num + func.operand;
      case '-':
        return num - func.operand;
      case '*':
        return num * func.operand;
      case '/':
        return parseInt(num / func.operand);
    }
  }
}

function plus(num) {
  return {
    operator: '+',
    operand: num
  };
}
function minus(num) {
  return {
    operator: '-',
    operand: num
  };
}
function times(num) {
  return {
    operator: '*',
    operand: num
  };
}
function dividedBy(num) {
  return {
    operator: '/',
    operand: num
  };
}

 

테스트 결과)

 

마음에 드는 다른 사람 소스)

728x90
반응형

'Algorithm > codewars' 카테고리의 다른 글

Counting Duplicates 답안  (0) 2021.03.24
Valid Braces 답안  (0) 2021.03.24
Is this a triangle? 답안  (0) 2021.03.23
Exes and Ohs 답안  (0) 2021.03.23
You're a square! 답안  (0) 2021.03.23