forked from marcusp619/js-tuesday-calc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc.js
54 lines (47 loc) · 1.09 KB
/
calc.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
$(document).ready(function() {
var operationQueue = [];
var add = function(a, b) {
var c = parseInt(a), d = parseInt(b);
return c + d;
};
var multiply = function(a, b) {
return a * b;
};
var divide = function(a, b) {
if (b !== 0) {
return a / b;
} else {
return "Err";
}
};
var subtract = function(a, b) {
return a - b;
};
var performOperation = function(operation, a, b){
if(operation==="+"){
return add(a, b);
} else if(operation==="-") {
return subtract(a, b);
} else if(operation==="x") {
return multiply(a, b);
} else {
return divide(a, b);
}
};
$('.key').click(function() {
var value = $(this).html();
if(value === "="){
a = operationQueue.shift();
operation = operationQueue.shift();
b = operationQueue.shift();
result = performOperation(operation, a, b);
operationQueue.push(result);
} else {
operationQueue.push(value);
}
if(value === "C"){
operationQueue = [];
}
$( '#ops-queue' ).html(operationQueue);
});
});