Description
- Your task is to create a function that does four basic mathematical operations.
- 네 가지 기본 수학 연산을 수행하는 함수를 작성한다.
Solution 01
function mathOperation(operation, value1, value2) {
if (operation === '+') return value1 + value2;
if (operation === '-') return value1 - value2;
if (operation === '*') return value1 * value2;
if (operation === '/') return value1 / value2;
}
mathOperation('+', 4, 4); // 8
mathOperation('-', 8, 2); // 6
mathOperation('*', 2, 4); // 8
mathOperation('/', 16, 2); // 8
Solution 02
function mathOperation(operation, value1, value2) {
switch (operation) {
case '+': return value1 + value2;
case '-': return value1 - value2;
case '*': return value1 * value2;
case '/': return value1 / value2;
}
}
mathOperation('+', 4, 4); // 8
mathOperation('-', 8, 2); // 6
mathOperation('*', 2, 4); // 8
mathOperation('/', 16, 2); // 8
Solution 03
function mathOperation(operation, value1, value2) {
let obj = {
'+': value1 + value2,
'-': value1 - value2,
'*': value1 * value2,
'/': value1 / value2
};
return obj[operation];
}
mathOperation('+', 4, 4); // 8
mathOperation('-', 8, 2); // 6
mathOperation('*', 2, 4); // 8
mathOperation('/', 16, 2); // 8
Solution 04
function mathOperation(operation, value1, value2) {
return eval(value1 + operation + value2);
}
mathOperation('+', 4, 4); // 8
mathOperation('-', 8, 2); // 6
mathOperation('*', 2, 4); // 8
mathOperation('/', 16, 2); // 8