Do I get a bonus?

It's bonus time in the big city! The fatcats are rubbing their paws in anticipation... but who is going to make the most money?

Build a function that takes in two arguments (salary, bonus). Salary will be an integer, and bonus a boolean.

If bonus is true, the salary should be multiplied by 10. If bonus is false, the fatcat did not make enough money and must receive only his stated salary.

Return the total figure the individual will receive as a string prefixed with "£" (= "\u00A3", JS and Java) or "$" (C#, C++, Ruby, Clojure, Elixir, PHP and Python, Haskell).

문제 풀이

function bonusTime(salary, bonus) {
  if (bonus) {
    return "\u00A3" + String(salary*10)
    }
  else {
    return "\u00A3" + String(salary)
    }
}

깔끔한 문제풀이

function bonusTime(salary, bonus) {
  return bonus ? `£${10 * salary}` : `£${salary}`;
}

중요개념

  • if ... else 문을 3항 연산자로 바꾸기
    • condition ? expr1 : expr2
    • condition: true 혹은 false 평가되는 표현식
    • expr1, expr2: 모든 형식의 값을 지닌 표현식
    • 삼항 연산자는 if / else 문을 사용하는 함수를 반환하는 데 적합
    • 다중으로 삼항 연산자 가능