/
Chapter 2.txt
256 lines (182 loc) · 5.58 KB
/
Chapter 2.txt
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
Listing 2-1. Declaring a Variable
var num0;
var num1;
var sum;
Listing 2-2. Assigning Values to Variables
num0 = 100;
num1 = 200;
Listing 2-3. Adding Variables
sum = num0 + num1;
Listing 2-4. Changing the Order of Operations with Parentheses
var result0 = 1 + 2 * 3; //result0 will be 7
var result1 = (1 + 2) * 3; //result1 will be 9
Listing 2-5. Displaying the Value of Sum on the Screen
document.write(“Total amount = “ + sum);
Listing 2-6. Manipulating a Variable by Referencing Itself
var score = 10;
score = score + 20;
document.write(score);
Listing 2-7. Declaring and Incrementing a Variable
var num = 1;
num++;
document.write(num);
Listing 2-8. Incrementing a Variable within a Statement
var num = 1;
document.write(num++);
num = 1;
document.write(++num);
Listing 2-9. Comparing Values
var num0 = 4;
var num1 = 7;
document.write(num0 >= num1);
document.write(num0 < num1);
document.write(num0 === num1);
Listing 2-10. Differing Equivalent Operators
var num0 = 4;
var num1 = “4”;
document.write(num0 == num1);
document.write(num0 === num1);
Listing 2-11. Manipulating Comparisons
document.write(!(4 > 3));
document.write((4 < 7) && (5 < 13));
document.write((4 > 5) || (4 < 5));
Listing 2-12. Implementing Logic with the If Statement
var num0 = 4;
var num1 = 8;
if (num0 >= num1) {
document.write(num0 + “ is greater than or equal to ” + num1 + “.”);
}
else if (num0 < num1) {
document.write(num0 + “ is less than ” + num1 + “.”);
}
Listing 2-13. Creating the Date Object
var date = new Date();
Listing 2-14. Assigning Date Information to Variables
var y = date.getFullYear();
var m = date.getMonth() + 1;
var d = date.getDate();
Listing 2-15. Creating a String and Displaying It
var text = "The date is " + m + "/" + d + "/" + y;
document.write(text);
Listing 2-16. An Impractical Way to Add Sequential Numbers
int num = 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;
Listing 2-17. Creating Variables
var i;
var sumFor;
var sumWhile;
Listing 2-18. The While Loop
sumWhile = 0;
i = 1;
while (i <= 100) {
sumWhile += i;
i++;
}
document.write(“The result using a while loop: ” + sumWhile + “<BR />”);
Listing 2-19. The For Loop
sumFor = 0;
for (i = 1; i <= 100; i++) {
sumFor += i;
}
document.write("The result using a for loop: " + sumFor + “<BR />”);
Listing 2-20. Breaking Out of A Loop
var sumWhile = 0;
var i = 1;
while (true) {
sumWhile += i;
i++;
if (i > 100) break;
}
document.write(sumWhile);
Listing 2-21. Using Continue to Skip Odd Numbers
var sumFor = 0;
for (i = 1; i <= 100; i++) {
if (i % 2 != 0) continue;
sumFor = sumFor + i;
}
document.write(“The sum of all even numbers up to 100 is ” + sumFor);
Listing 2-22. Creating Variables for the Fortune-Telling Game
var num;
var text = "";
num = Math.floor(Math.random() * 4);
Listing 2-23. Fortune Telling with the If Statement
if (num===0) {
text = "Super Lucky";
} else if (num===1) {
text = "So-so Luck";
} else if (num===2) {
text = "Minor Luck";
} else {
text = "Bad Luck";
}
document.write("Your fortune: " + text);
Listing 2-24. Setting Up for the Switch Example
var num;
var text = "";
num = Math.floor(Math.random() * 4);
Listing 2-25. The Fortune-Telling Switch Statement
switch (num) {
case 0:
textSwitch = "Excellent Luck";
break;
case 1:
textSwitch = "Moderate Luck";
break;
case 2:
textSwitch = "Small Luck";
break;
default:
textSwitch = "Bad Luck";
break;
}
Listing 2-26. Creating an Array of Devices
var array = new Array(3);
array[0] = "iPhone";
array[1] = "Android";
array[2] = "Computer";
Listing 2-27. Iterating Through an Array
var text = "";
for (var i = 0; i < array.length; i++) {
text += array[i] + "<BR />";
}
document.write(text);
Listing 2-28. Creating an Associative Array
var obj = new Object();
obj["iPhone"] = "White";
obj["Android"] = "Black";
obj["Computer"] = "Silver";
Listing 2-29. Manually Iterating Through the Associative Array
var textObj = "";
textObj += “The iPhone is ” + obj["iPhone"] + ".<BR />";
textObj += “The Android is ” + obj["Android"] + ".<BR />";
textObj += “The Computer is ” + obj["Computer"] + ".<BR />";
document.write(textObj);
Listing 2-30. Defining the buy() Function
function buy(device) {
document.write("I will buy the " + device + ".<BR />");
}
Listing 2-31. Calling the Function
buy(“iPhone”);
buy(“Android”);
Listing 2-32. Making King Bob
var bob = 39;
Listing 2-33. Creating a Function to Create a Random Bob
function villager() {
var bob = Math.floor(Math.random() * 50 + 10);
this.bob += 1;
document.write(“King Bob’s age is ” + bob + “.<BR />”);
document.write(“Villager Bob, this Bob, is ” + this.bob + “.”);
}
Listing 2-34. Calling the Villager Function
Villager();
Listing 2-35. Creating the Prototype and Properties
function Software() {
}
Listing 2-36. Creating The Prototype Function
Software.prototype.output = function() {
document.write(this.name + " is written in " + this.language + "<BR />");
};
Listing 2-37. Creating the Accounting Object
var accounting = new Software();
accounting.name = "Spreadsheet Master";
accounting.language = "Java";
accounting.output();