-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.js
63 lines (39 loc) · 1.09 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
63
$('button').click(function() {
if($('#screen').val() == 0) {
$('#screen').val('');
}
y = $('#screen').val()
let x = $(this).attr('id').toLowerCase();
switch(x) {
case '=' :
if(y == '0' || y == '') {
$('#screen').val('Nope!')
setTimeout(function() {
$('#screen').val('0')
}, 500)
break
}
else {
let value = eval(y).toString();
$('#screen').val("Ans = " + value)
break
}
case 'del':
if(y != '0') {
$('#screen').val(y.slice(0, -1))
}
break
case 'c':
$('#screen').val(0)
break
default:
$('#screen').val(y + x)
}
});
$('input').on('keyup keydown', function() {
let allowed = ['0','1','2','3','4','5','6','7','8','9','c','del','=','*','/'];
x = $(this).val()
if (allowed.indexOf($(this).val().slice(-1)) == -1) {
$(this).val(x.slice(0,-1))
}
})