-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathematicalOperations.js
More file actions
34 lines (29 loc) · 974 Bytes
/
MathematicalOperations.js
File metadata and controls
34 lines (29 loc) · 974 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//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.
// Examples(Operator, value1, value2) --> output
// ('+', 4, 7) --> 11
// ('-', 15, 18) --> -3
// ('*', 5, 5) --> 25
// ('/', 49, 7) --> 7
//My Answer
function basicOp(operation, value1, value2) {
if (operation === "+") {
return value1 + value2;
} else if (operation === "-") {
return value1 - value2;
} else if (operation === "*") {
return value1 * value2;
} else if (operation === "/") {
return value1 / value2;
} else {
return "Type in either + - / * for operation";
}
}
//Me testing it out
console.log(basicOp("+", 1, 2));
//Really smarter way of doing it is using eval
// function basicOp(o, a, b) {
// return eval(a+o+b);
// }