-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.js
62 lines (58 loc) · 1.38 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
(function( app ){
'use strict';
var _app = app || {};
var Calculator = function() {
};
Calculator.prototype = {
constructor : Calculator,
add : function( n1, n2 ) {
var self = this, sum;
if( !isNaN(n1) && !isNaN(n2) ) {
sum = n1 + n2;
}
return sum;
},
addAll : function() {
var self = this, result = 0, currElem,
allCount = arguments.length;
if( allCount ) {
for (var i = 0; i < allCount; i++) {
currElem = arguments[i];
if( !isNaN( currElem )) {
result = result + currElem;
}
else if( Array.isArray( currElem ) && currElem.length ) {
result = result + currElem.reduce(function( prev, curr ) {
return prev + curr;
},0);
}
};
}
return result;
},
subtract : function( n1, n2 ) {
var self = this, result;
if( !isNaN(n1) && !isNaN(n2) ) {
result = n1 -n2;
}
return result;
},
divide : function( n1, n2 ) {
var self, result;
if( !isNaN(n1) && !isNaN(n2) ) {
result = n1/n2;
}
return result;
},
average : function() {
var self = this, result, allSum = 0, allCount;
allCount = arguments.length;
if( allCount ) {
allSum = self.addAll.call(this, Array.from(arguments));
result = allSum/allCount;
}
return result;
}
};
_app.Calculator = Calculator;
})( myApp || window.myApp );