-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
53 lines (40 loc) · 858 Bytes
/
app.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
$(document).ready(function(){
$('.equals').click(execute);
$('.calc input[type="button"]:not(.equals):not(.backspace)').click(input);
$('.backspace').click(backspace);
$('.erase').click(erase);
$(document).on('keypress', onKey);
$statement.focus();
});
var $statement = $('#statement')[0];
function input(){
$statement.value += this.value;
$statement.focus();
}
function backspace(){
$statement.value = $statement.value.slice(0,-1);
$statement.focus();
}
function erase(){
$statement.value='';
$statement.focus();
}
function execute(){
var s = $statement.value;
try{
var array = parseStatement(s);
$statement.value = validateAndReduceArray(array);
}
catch(err){
alert(err);
}
$statement.focus();
}
function onKey(e){
$statement.focus();
//ENTER
if(e.keyCode == 13){
e.preventDefault();
execute();
}
}