forked from conmeconte/calc_state_engine_example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
147 lines (121 loc) · 3.17 KB
/
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
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
130
131
132
133
$(document).ready(initiate_click_handlers_SE);
var inputs = [''];
var input_index = 0;
var old_num2 = null;
var old_op = null;
function initiate_click_handlers(){
$(".number").click(function(){
var text = $(this).text();
number_click(text);
});
$('.operator').click(function(){
var text = $(this).text();
operator_click(text);
});
$('.equals').click(function(){
equals_click();
});
}
function initiate_click_handlers_SE(){
$(".number").click(function(){
var text = $(this).text();
current_state.number_clicked(text);
});
$('.operator').click(function(){
var text = $(this).text();
current_state.operator_clicked(text);
});
$('.equals').click(function(){
current_state.equals_clicked();
});
}
function number_click(text){
inputs[input_index]+=text;
}
function operator_click(text){
input_index++;
inputs[input_index] = text;
input_index++;
inputs[input_index] = '';
}
function equals_click(){
console.log('the answer is '+(parseInt(inputs[0]) + parseInt(inputs[2])));
}
// function equals_click(){
// console.log(inputs);
// if(inputs[0]!=''){
// if(inputs[1]!=undefined){
// if(inputs[2]!=''){
// } else {
// inputs[2] = inputs[0];
// }
// } else {
// if(old_num2!=null && old_op!=null){
// inputs[1] = old_op;
// inputs[2] = old_num2;
// }
// }
// do_math();
// }
// }
function noop(){
console.log('cannot do that now');
}
var state_modules = {
initial_calculator : {
name: 'initial_calculator',
number_clicked: function(text){
number_click(text);
current_state = state_modules.number_pressed;
},
operator_clicked: noop,
equals_clicked: noop
},
number_pressed : {
name: 'number_pressed',
number_clicked: number_click,
operator_clicked: function(text){
operator_click(text);
current_state = state_modules.number_and_op_pressed;
},
equals_clicked: noop
},
number_and_op_pressed : {
name: 'number and op pressed',
number_clicked: function(text){
number_click(text);
current_state = state_modules.number_and_op_and_number_pressed;
},
operator_clicked: noop,
equals_clicked: noop,
},
number_and_op_and_number_pressed : {
name: 'two numbers and operator pressed',
number_clicked: number_click,
operator_clicked: noop,
equals_clicked: function(){
equals_click();
current_state.update_display();
current_state = state_modules.initial_calculator;
},
update_display: function(){
console.log('update called');
console.log(inputs);
}
}
}
var current_state = state_modules.initial_calculator;
//initial calculator : nothing pressed
// numbers to add normally
// operator to do nothing
// equals to do nothing
//number pressed : first number exists
// number to add normally
// operator adds operator
//switches state to "number and op pressed" state
// equals to do nothing
//number and op pressed: first number and operator exist
// number to add normally to next number
// operator to do nothing
// equals : calculators the equation
//switches state to "initial calculator" state