-
-
Save nielsbom/89e370be3acd9330ebabfa3c0fcaa72f to your computer and use it in GitHub Desktop.
Use variants
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
https://www.codewars.com/kata/basic-mathematical-operations | |
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. | |
basicOp('+', 4, 7) // Output: 11 | |
basicOp('-', 15, 18) // Output: -3 | |
basicOp('*', 5, 5) // Output: 25 | |
basicOp('/', 49, 7) // Output: 7 | |
*/ | |
type operator = Plus | Minus | Multiply | Divide; | |
let basicOp3 = (operation, a, b) => { | |
switch (operation) { | |
| Plus => a + b | |
| Minus => a - b | |
| Multiply => a * b | |
| Divide => a / b | |
} | |
}; | |
Js.log(basicOp3(Plus, 4, 7) == 11); | |
Js.log(basicOp3(Minus, 15, 18) == -3); | |
Js.log(basicOp3(Multiply, 5, 5) == 25); | |
Js.log(basicOp3(Divide, 49, 7) == 7); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment