-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLesson 6 - Input Methods.js
185 lines (154 loc) · 4.88 KB
/
Lesson 6 - Input Methods.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//Defining stuff
var number1Input, number2Input;
//bonus 2 input
var number3Input;
//drop down
var operatorSelector, operatorSelector2;
//Currently selected operator
var operator, operator2;
//numbers
var number1, number2, number3;
var bonus2Array;
function setup() {
createCanvas(900, 900);
//Creating the input part of the calculator
number1Input = createInput("Number 1");
number1Input.position(100, 100);
number2Input = createInput("Number 2");
number2Input.position(350, 100);
//Bonus 2 input
number3Input = createInput("Bonus input");
number3Input.position(600, 100);
//Operator drop down
operatorSelector = createSelect();
operatorSelector2 = createSelect();
// Options
operatorSelector.option("+");
operatorSelector.option("-");
operatorSelector.option("*");
operatorSelector.option("/");
operatorSelector.option("^");
operatorSelector.option("√");
operatorSelector2.option(" ");
operatorSelector2.option("+");
operatorSelector2.option("-");
operatorSelector2.option("*");
operatorSelector2.option("/");
operatorSelector.position(300, 100);
operatorSelector2.position(550, 100);
}
function draw() {
//clearing the old numbers off the screen
clear();
operator = operatorSelector.value()
operator2 = operatorSelector2.value()
number1 = Number(number1Input.value());
number2 = Number(number2Input.value());
number3 = Number(number3Input.value());
//only dealing with 2 numbers
if (operator2 == " ") {
//checking the value of the selected operator
if (operator == "+") {
//Calculating and displaying the result
text(number1 + number2, 300, 200);
}
else if (operator == "-") {
//Calculating and displaying the result
text(number1 - number2, 300, 200);
}
else if (operator == "*") {
//Calculating and displaying the result
text(number1 * number2, 300, 200);
}
else if (operator == "/") {
//Calculating and displaying the result
text(number1 / number2, 300, 200);
}
else if (operator == "^") {
//Calculating and displaying the result
text(Math.pow(number1, number2), 300, 200);
}
else if (operator == "√") {
//Calculating and displaying the result
text(Math.sqrt(number1), 300, 200);
}
}
//javascript should auto follow the order of operations so we can just let the program do the math in the text()
//there is probably a much cleaner and less spaghetti way of doing it
else if (operator2 == "+") {
//checking the value of the selected operator
if (operator == "+") {
//Calculating and displaying the result
text(number1 + number2 + number3, 300, 200);
}
else if (operator == "-") {
//Calculating and displaying the result
text(number1 - number2 + number3, 300, 200);
}
else if (operator == "*") {
//Calculating and displaying the result
text(number1 * number2 + number3, 300, 200);
}
else if (operator == "/") {
//Calculating and displaying the result
text(number1 / number2 + number3, 300, 200);
}
}
else if (operator2 == "-") {
//checking the value of the selected operator
if (operator == "+") {
//Calculating and displaying the result
text(number1 + number2 - number3, 300, 200);
}
else if (operator == "-") {
//Calculating and displaying the result
text(number1 - number2 - number3, 300, 200);
}
else if (operator == "*") {
//Calculating and displaying the result
text(number1 * number2 - number3, 300, 200);
}
else if (operator == "/") {
//Calculating and displaying the result
text(number1 / number2 - number3, 300, 200);
}
}
else if (operator2 == "*") {
//checking the value of the selected operator
if (operator == "+") {
//Calculating and displaying the result
text(number1 + number2 * number3, 300, 200);
}
else if (operator == "-") {
//Calculating and displaying the result
text(number1 - number2 * number3, 300, 200);
}
else if (operator == "*") {
//Calculating and displaying the result
text(number1 * number2 * number3, 300, 200);
}
else if (operator == "/") {
//Calculating and displaying the result
text(number1 / number2 * number3, 300, 200);
}
}
else if (operator2 == "/") {
//checking the value of the selected operator
if (operator == "+") {
//Calculating and displaying the result
text(number1 + number2 / number3, 300, 200);
}
else if (operator == "-") {
//Calculating and displaying the result
text(number1 - number2 / number3, 300, 200);
}
else if (operator == "*") {
//Calculating and displaying the result
text(number1 * number2 / number3, 300, 200);
}
else if (operator == "/") {
//Calculating and displaying the result
text(number1 / number2 / number3, 300, 200);
}
}
}