-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
129 lines (122 loc) · 2.87 KB
/
script.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
123
124
125
126
127
128
129
$(document).ready(function() {
var $keys = $('.calculator button');
var $screen = $('.screen');
var decimal = false;
var operators = ['+', '-', 'x', '÷'];
$keys.click(function() {
var keyVal = $(this).data('val');
output = $('.screen').html();
console.log(keyVal);
// clear
if (keyVal == 'clear') {
$screen.html('');
decimal = false;
}
// equal
else if (keyVal == '=') {
var equation = output;
var lastChar = equation[equation.length - 1];
equation = equation.replace(/x/g, '*').replace(/÷/g, '/');
if (operators.indexOf(lastChar) > -1 || lastChar == '.')
equation = equation.replace(/.$/, '');
if (equation) {
$screen.html(eval(equation));
}
decimal = false;
}
// operators
else if ($(this).parent().is('.operators')) {
var lastChar = output[output.length - 1];
if (output != '' && operators.indexOf(lastChar) == -1) {
$screen.html($screen.html() + keyVal);
} else if (output == '' && keyVal == '-') {
$screen.html($screen.html() + keyVal);
}
if (operators.indexOf(lastChar) > -1 && output.length > 1) {
$screen.html($screen.html().replace(/.$/, keyVal));
}
decimal = false;
}
// decimal
else if (keyVal == '.') {
if (!decimal) {
$screen.html($screen.html() + keyVal);
decimal = true;
}
}
// buttons
else {
$screen.html($screen.html() + keyVal);
}
});
$(window).keydown(function(e) {
console.log(e.which);
switch (e.which) {
case 96:
key = 0;
break;
case 97:
key = 1;
break;
case 98:
key = 2;
break;
case 99:
key = 3;
break;
case 100:
key = 4;
break;
case 101:
key = 5;
break;
case 102:
key = 6;
break;
case 103:
key = 7;
break;
case 104:
key = 8;
break;
case 105:
key = 9;
break;
case 111:
key = '÷';
break;
case 109:
key = '-';
break;
case 106:
key = 'x';
break;
case 107:
key = '+';
break;
case 13:
key = '=';
break;
case 110:
key = '.';
break;
case 27:
key = 'clear';
break;
default:
return false;
}
$('[data-val="' + key + '"]').addClass('active').click();
}).keyup(function() {
$('.active').removeClass('active');
});
$('[data-val="clear"]').click().delay(100).queue(function() {
$('[data-val="1"]').click().delay(200).queue(function() {
$('[data-val="5"]').click().delay(200).queue(function() {
$('[data-val="x"]').click().delay(200).queue(function() {
$('[data-val="3"]').click();
});
});
});
});
});