-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
29 lines (26 loc) · 806 Bytes
/
test.js
File metadata and controls
29 lines (26 loc) · 806 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
// calculator.js
function calculate(firstNumber, secondNumber, operation) {
let result;
switch(operation) {
case '+':
result = parseFloat(firstNumber) + parseFloat(secondNumber);
break;
case '-':
result = parseFloat(firstNumber) - parseFloat(secondNumber);
break;
case '*':
result = parseFloat(firstNumber) * parseFloat(secondNumber);
break;
case '/':
if(secondNumber != '0') {
result = parseFloat(firstNumber) / parseFloat(secondNumber);
} else {
throw new Error('Division by zero is not allowed.');
}
break;
default:
throw new Error('Invalid operation! Please enter either +, -, *, or /.');
}
return result;
}
module.exports = calculate;