-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
90 lines (90 loc) · 2.15 KB
/
index.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
function calculate() {
let result = 0;
function add(...rest) {
for(let i = 0; i < rest.length; i++){
let value = rest[i];
result += value;
}
return result;
}
function subtract(...rest) {
for(let i = 0; i < rest.length; i++) {
if(i === 0) {
if(result === 0){
result = rest[0];
} else {
result = result - rest[0];
}
} else {
result -= rest[i];
}
}
return result;
}
function multiply(...rest) {
if(result === 0){
result = 1;
}
for(let i = 0; i < rest.length; i++){
let value = rest[i];
result *= value;
}
return result;
}
function divide(...rest) {
for(let i = 0; i < rest.length; i++){
if(i === 0){
if(result === 0){
result = rest[0];
} else {
result = result / rest[0];
}
} else {
result /= rest[i];
}
}
return result;
}
function getResult() {
return result;
}
function clear(){
result = 0;
return result;
}
return {
add: add,
subtract: subtract,
multiply: multiply,
divide: divide,
getResult: getResult,
clear: clear
}
}
let calculator = calculate();
console.log(calculator.add(5));
console.log(calculator.add(7));
console.log(calculator.getResult());
console.log(calculator.clear());
console.log(calculator.add(5));
console.log(calculator.getResult());
console.log(calculator.add(5, 6, 2));
console.log(calculator.getResult());
console.log(calculator.clear());
console.log(calculator.subtract(3, 7, 10, 7));
console.log(calculator.getResult());
console.log(calculator.subtract(-15, 7));
console.log(calculator.getResult());
console.log(calculator.add(11, 7));
console.log(calculator.getResult());
console.log(calculator.clear());
console.log(calculator.multiply(3, -5, -10));
console.log(calculator.getResult());
console.log(calculator.divide(10));
console.log(calculator.getResult());
console.log(calculator.clear());
console.log(calculator.divide(10, 5, 2, 2));
console.log(calculator.getResult());
console.log(calculator.add(10, 7));
console.log(calculator.getResult());
console.log(calculator.clear());