-
Notifications
You must be signed in to change notification settings - Fork 0
/
03-basic-calculator.htm
184 lines (134 loc) · 6.08 KB
/
03-basic-calculator.htm
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
<!DOCTYPE html>
<html>
<head>
<title>Basic Calculator</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- LIBRARY FILES -->
<link rel="stylesheet" type="text/css" href="basic/basic.min.css">
<script src="basic/basic.min.js" type="text/javascript" charset="utf-8"></script>
<script>
/*
ALGORITHM:
- With two TextBoxes, numbers to be processed are requested.
- Four Buttons are used to perform mathematical operations.
- The result of the action is displayed in another TextBox.
PROJECT DESCRIPTION:
A step-by-step video of the project. (Language: Turkish)
https://youtu.be/fAMWzewpE1Q
*/
// Input and result TextBoxes.
var txtNum1
var txtNum2
var txtResult
// Action Buttons.
var btnSum
var btnMinus
var btnMultiply
var btnDivide
// First run function.
var start = function() {
// Set the background color of the page as white smoke.
page.color = "whitesmoke"
// TEXTBOX: Create a TextBox (TextBox) for the first number input.
txtNum1 = createTextBox()
// For it to be perceived as text, let's write it in quotes.
txtNum1.text = "0"
txtNum1.title = "Number 1:"
txtNum1.left = 20
txtNum1.top = 50
// TEXTBOX: Create TextBox for second number input.
txtNum2 = createTextBox()
txtNum2.text = "0"
txtNum2.title = "Number 2:"
txtNum2.left = txtNum1.left
// Place this object 40px (pixels) below the txtNum1 object.
txtNum2.top = txtNum1.top + txtNum1.height + 40
// BUTTON: Create addition button.
btnSum = createButton()
btnSum.text = "Sum"
btnSum.left = txtNum1.left
// Place this object 10px below the txtNum2 object.
btnSum.top = txtNum2.top + txtNum2.height + 10
btnSum.onClick(doAddition)
// BUTTON: Create subtraction button.
btnMinus = createButton()
btnMinus.text = "Minus"
// Place this object 10px to the right of the btnSum object.
btnMinus.left = btnSum.left + btnSum.width + 10
btnMinus.top = btnSum.top
// Change the color of the object to tomato red.
btnMinus.color = "tomato"
btnMinus.onClick(doSubtraction)
// BUTTON: Create multiplication button.
btnMultiply = createButton()
btnMultiply.text = "Multiply"
btnMultiply.left = txtNum1.left
// Place this object 10px below the btnSum object.
btnMultiply.top = btnSum.top + btnSum.height + 10
btnMultiply.color = "lightgray"
btnMultiply.onClick(doMultiplication)
// BUTTON: Create division button.
btnDivide = createButton()
btnDivide.text = "Divide"
// Let the object be the same left space as the btnMinus object.
btnDivide.left = btnMinus.left
// Let the object be the same top space as the btnMultiply object.
btnDivide.top = btnMultiply.top
btnDivide.color = "lightgray"
btnDivide.onClick(doDivision)
// TEXTBOX: Create TextBox for result.
txtResult = createTextBox()
txtResult.title = "Result:"
txtResult.left = txtNum1.left
txtResult.top = btnMultiply.top + btnMultiply.height + 40
}
// Addition function.
var doAddition = function() {
// Perform the addition and write the result in the TextBox.
txtResult.text = num(txtNum1.text) + num(txtNum2.text)
// NOTE: num(text) function,
// parameter sent as text,
// returns the converted number.
// EXAMPLE USES:
// num("5") answer: 5
// num("5.5") answer: 5.5
// num("5.5", "integer") answer: 5
// str(5) answer: "5"
// NOTE: If the str(number) function is
// parameter sent as a number,
// returns the translated version of the text.
}
// Subtraction function.
var doSubtraction = function() {
// Write the result in the TextBox.
txtResult.text = num(txtNum1.text) - num(txtNum2.text)
}
// Multiplication function.
var doMultiplication = function() {
// Write the result in the TextBox.
txtResult.text = num(txtNum1.text) * num(txtNum2.text)
}
// Division function.
var doDivision = function() {
// Write the result in the TextBox.
txtResult.text = num(txtNum1.text) / num(txtNum2.text)
// Note: Converting decimal to integer:
// The answer to 3 / 2 is a decimal number. 1.5
// The num("1.5", "integer") function converts the result to an integer. 1
// txtResult.text = num(num(txtNum1.text) / num(txtNum2.text), "integer")
}
/*
DEVELOPMENT SUGGESTIONS:
- Each action button is clicked, the title (.title) of the result TextBox may change.
"Result of addition:", "Result of subtraction:" etc.
- The result can be displayed as a full transaction. 2 + 1 = 3
- Continue operation button can be added. Writes the result in the first TextBox
and clears the second TextBox.
*/
</script>
</head>
<body>
<!-- HTML content -->
</body>
</html>