-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
40 lines (37 loc) · 870 Bytes
/
main.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
var tmpResult = 0;
var tmpOperator;
var operation;
$(".calc__key").click(function() {
var newInput = $(this).text();
var oldInput = $(".calc__display").text();
if ($(this).hasClass("calc__key--operator")) {
tmpResult = parseInt(oldInput);
tmpOperator = newInput;
$(".calc__display").text("");
}
else if($(this).hasClass("calc__key--total")) {
switch (tmpOperator) {
case '+':
operation = (tmpResult + parseInt(oldInput));
break;
case '-':
operation = (tmpResult - parseInt(oldInput));
break;
case 'x':
operation = (tmpResult * parseInt(oldInput));
break;
case '/':
operation = (tmpResult / parseInt(oldInput));
break;
}
$(".calc__display").text(operation);
}
else {
if (oldInput === "0") {
$(".calc__display").text(newInput);
}
else {
$(".calc__display").text(oldInput + newInput);
}
}
});