forked from devleague/js-calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.js
123 lines (101 loc) · 2.12 KB
/
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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* Declare a function named `calculatorModule`
* this function will have two private variables declared inside of it.
* @variable PRIVATE { Number } `memory`
* @variable PRIVATE { Number } `total`
* @return {object} `calculator` object that can be used
*/
/**
* sets the `total` to the number passed in
* @param { Number } x
* @return { Number } current total
*/
/**
* Return the value of `total`
* @return { Number }
*/
function calculatorModule(x){
//private
var memory = 0;
var total = 0;
var isValid = function(x) {
if(typeof x === "number") {
return true;
} else {
throw new Error("not a number");
}
}
//public
return {
load: function(x) {
if(isValid(x)){
total = x;
return total;
}
},
getTotal: function() {
return total;
},
add: function(x) {
if(isValid(x)){
return total += x;
}
},
subtract: function(x) {
if(isValid(x)){
return total -= x;
}
},
multiply: function(x) {
if(isValid(x)){
return total *= x;
}
},
divide: function(x) {
if(isValid(x)){
return total /= x;
}
},
recallMemory: function() {
return memory;
},
saveMemory: function() {
memory = total;
},
clearMemory: function() {
memory = 0;
}
};
}
// var myCalculator = calculatorModule();
// myCalculator.load()
// console.log(myCalculator.getTotal());
/**
* Sums the value passed in with `total`
* @param { Number } x
*/
/**
* Subtracts the value passed in from `total`
* @param { Number } x
*/
/**
* Multiplies the value by `total`
* @param { Number } x
*/
/**
* Divides the value passing in by `total`
* @param { Number } x
*/
/**
* Return the value stored at `memory`
* @return { Number }
*/
/**
* Stores the value of `total` to `memory`
*/
/**
* Clear the value stored at `memory`
*/
/**
* Validation
*/