[codewars] Basic Mathematical Operations

KJA·2022년 8월 19일
0

https://www.codewars.com/kata/57356c55867b9b7a60000bd7


Description

Your task is to create a function that does four basic mathematical operations.

The function should take three arguments - operation(string/char), value1(number), value2(number).
The function should return result of numbers after applying the chosen operation.

Examples

(Operator, value1, value2) --> output

('+', 4, 7) --> 11
('-', 15, 18) --> -3
('*', 5, 5) --> 25
('/', 49, 7) --> 7

문제 해결

function basicOp(operation, value1, value2) {
    let answer = 0;
    switch (operation) {
        case '+':
            answer = value1 + value2;
            break;
        case '-':
            answer = value1 - value2;
            break;
        case '*':
            answer = value1 * value2;
            break;
        case '/':
            answer = value1 / value2;
            break;
    }
    return answer;
}

다른 풀이

function basicOp(operation, value1, value2) {
  var cases = {
    '+': value1 + value2,
    '-': value1 - value2,
    '*': value1 * value2,
    '/': value1 / value2
  };
  return cases[operation]
}

1개의 댓글

comment-user-thumbnail
2023년 5월 2일

Maths can be very useful in life, and also if you want to work in the IT field, for example. Now children can learn maths from early childhood thanks to platforms like Brighterly, where teachers will make the best math programs for kids, and also organize the learning process in such a way that it doesn't bother the child and will be effective.

답글 달기