-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalc.js
More file actions
65 lines (59 loc) · 1.71 KB
/
calc.js
File metadata and controls
65 lines (59 loc) · 1.71 KB
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
/* var Math = Math || {};
Math.Calculator = function(id){
console.log("id = " + id);
};
var s = new Math.Calculator(123); */
var calculator = (function(id){
var el = document.getElementById(id);
var operation, x = '' , y = '', xSet = false, opDone = false;
return {
calculate : function(){
var result = 0;
switch (operation){
case '+' :
result = parseInt(x) + parseInt(y);
break;
case '-' :
result = parseInt(x) - parseInt(y);
break;
case '*' :
result = parseInt(x) * parseInt(y);
break;
case '/' :
if(y == 0){
alert("Cannot divide by zero!");
return;
}
result = parseInt(x) / parseInt(y);
break;
}
console.log("x = " + x + ", y = " + y);
opDone = true;
el.innerText = result;
x = result;
y = "";
return result;
},
setOperation: function(oper){
operation = oper;
xSet = true;
opDone = false;
el.innerText += " " + oper;
},
setNumber: function(value){
if(opDone){
xSet = false;
opDone = false;
x = "";
el.innerText = x;
}
if(xSet){
y += value;
el.innerText += value;
}else{
x += value;
el.innerText += value;
}
}
};
}("output"));