-
Notifications
You must be signed in to change notification settings - Fork 0
/
Addition_subtraction_Division_Production_Numbers
49 lines (44 loc) · 1.68 KB
/
Addition_subtraction_Division_Production_Numbers
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
39
40
41
42
43
44
45
46
47
48
49
//Find sum, subtraction, Product, Division
function common(...values) {
var filterValue = values.filter((item) => typeof item == "number");
if(filterValue.length < 1) {
throw new Error("All Entered arguments are not valid numbers");
}
return filterValue;
}
function commonError(...values) {
if((typeof values[0] != "number") || (values[0] == 0))
throw new Error ("Please enter non zero number ");
}
const sum = (...rest) => {
const validItems = common(...rest);
const totalValue = validItems.reduce( (total, currentValue) => total + currentValue);
return totalValue;
};
console.log("Total : ",sum(1, 2, 3, undefined));
console.log("Total : ",sum(1, 2, undefined, 4));
console.log("Total : ",sum(undefined, 2, 0, undefined));
console.log("Total : ",sum(undefined, undefined, undefined, undefined));
const product = (...rest) => {
const validItems = common(...rest);
const prod = validItems.reduce( (prod, currentValue) => prod * currentValue);
return prod
};
//console.log("Product : ",product(undefined, 0, 3, 2));
const division = (...rest) => {
const validItems = common(...rest);
var filterValue = rest.filter((item) => typeof item == "number");
commonError(...rest)
const div = filterValue.reduce((div, currentValue) => div / currentValue);
return div;
}
//console.log("Division : ",division(0, 2, 3, 4))
const subtraction = (...rest) => {
const validItems = common(...rest);
commonError(...rest);
const sub = validItems.reduce((sub, currentValue) => sub - currentValue);
return sub;
}
//console.log("Subtraction : ",subtraction(20, 5, 3, 2));
//console.log("Subtraction : ",subtraction(undefined, 1, 3, 2));
//console.log("Subtraction : ",subtraction(0, 5, 3, 2));