-
-
Save nielsbom/6195651798b8d7ca2fdda9ea8253193f to your computer and use it in GitHub Desktop.
Basic mathematical operations in Reason
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 | |
*/ | |
let basicOp = (operation, a, b) => { | |
switch (operation) { | |
| '+' => a + b | |
| '-' => a - b | |
| '*' => a * b | |
| '/' => a / b | |
| _ => raise(Not_found) /* OK? */ | |
} | |
}; | |
Js.log(basicOp('+', 4, 7) == 11); | |
Js.log(basicOp('-', 15, 18) == -3); | |
Js.log(basicOp('*', 5, 5) == 25); | |
Js.log(basicOp('/', 49, 7) == 7); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment