forked from kevin-bennett-ags/tdd-homework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.js
38 lines (29 loc) · 795 Bytes
/
calculator.js
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
35
36
37
38
'use strict';
const NEGATIVE_ERROR = 'negatives not allowed',
ZERO_ERROR = 'zero is not allowed as divider';
exports.add = function(strArr) {
let numArr = strArr.split(',').map(Number),
total = 0;
for(let num of numArr) {
if(num < 0) throw new Error(NEGATIVE_ERROR)
total += num;
}
return total;
}
exports.multiply = function(val1, val2) {
return Number(val1) * Number(val2);
}
// behavior for divide
exports.divide = function(val1, val2) {
let num1 = Number(val1),
num2 = Number(val2);
if(num2 === 0) throw new Error(ZERO_ERROR)
return num1 / num2;
}
// bahavior for remainder
exports.remainder = function(val1, val2) {
let num1 = Number(val1),
num2 = Number(val2);
if(num2 === 0) throw new Error(ZERO_ERROR)
return num1 % num2;
}