-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlec-05-01-js-intro.js
More file actions
314 lines (244 loc) · 6.72 KB
/
lec-05-01-js-intro.js
File metadata and controls
314 lines (244 loc) · 6.72 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
const neverChange = "Project Key"; // comment
let oftenChanges = 0; // maybe we will change this value later
var oldDeclaration = 0; // we try to avoid using var now; use let instead
let first="Matt";
let last="Price"
console.log(first + last);
let n = 1,
m=2,
s="Hello, there! ";
console.log(m+n);
console.log(s+n);
let historians= ["Edward Gibbon", "Leopold von Ranke", "Edward Said", "Joan Scott"];
a = [];
a.push("Edward Gibbon");
a.push("Edward Said");
a.push("Joan Scott");
console.log(a[1]);
a.pop();
// a
wardates=[1776, 1792, 1812, 1861, 1870, 1914, 1939, 1994]
console.log("The time between The First and \
Second World Wars was " + (wardates[6] - wardates[5]) + " years");
let i = 0;
while (i < historians.length) {
console.log(historians[i] + " was a historian.");
i+=1;
}
for (let i=0; i < historians.length ; i++){
console.log(historians[i] + " was a historian.");
i // this is not required, just here to show you a slight difference
}
for (let i in historians) {
console.log(historians[i] + " was a historian.");
}
for (let h of historians) {
console.log(h + " was a historian.");
}
while (i < historians.length) {
if (historians[i] === "Joan Scott") {
console.log(historians[i] + " is my favourite.");
}
i +=1;
};
while (i < historians.length) {
if (historians[i] ==="Joan Scott") {
console.log(historians[i] + " is my favourite.");
} else {
console.log(historians[i] + ", meh.");
}
i +=1;
};
let a=["name", 0, "otherinfo"];
console.log(a[2]);
console.log(a.length);
console.log(a[length]);
a.pop;
function square(number) {
return number * number;
}
let cube = function(number) {
return number * number * number;
};
console.log(square(2));
console.log(cube(3));
historians= ["Edward Gibbon", "Leopold von Ranke", "Edward Said", "Joan Scott"];
i = 0;
while (i < historians.length) {
console.log(historians[i] + " was a historian.");
i+=1;
}
function makeSentences(historians) {
let i = 0,
output = "";
while (i < historians.length) {
output += historians[i] + " was a historian.\n";
i+=1;
}
return output;
}
let h1 = ["Edward Gibbon", "Leopold von Ranke", "Edward Said", "Joan Scott"],
h2 = ["Orlando Patterson", "Michel Foucault", "Natalie Zeemon Davis", "Howard Zinn"];
makeSentences(h1);
//makeSentences(h2);
a = 'just some string';
b= 'some other string';
function scopeExample (anyString) {
let a = 'I set this value inside the function';
return ('inside the function, a="' + a + '", not ' + anyString);
}
// a
// scopeExample(a);
//anyString
a = "global scope a";
console.log(a);
for (i=0; i<6; i++) {
let a = "local scope a on iteration: " + (i + 1) ;
console.log(a)
}
console.log(a);
function returnArray (first, second, third) {
// you can define the array using "new Array ()" or just "[ , , ]"
// don't forget to return it
// return ; // add the value here!
}
a = returnArray (1, 3,5);
a // quokka will display the value
function robotCleaner () {
let output = "I cleaned your room";
return output;
}
let r = robotCleaner();
a
function robotCleaner () {
let output = `Ha, ha! I have replaced your robot cleaner!
Now your room is even messier! Bwa ha ha ha ha!`
return output;
}
a = robotCleaner();
a
function greatWriter (name) {
let output = "Margaret Atwood was a great writer."
return output
}
console.log(greatWriter("Margaret Atwood"))
// console.log(greatWriter("Toni Morrison"))
function evenGreaterWriter (name) {
let output = name + " was a great writer."
return output
}
console.log(evenGreaterWriter("Margaret Atwood"))
// console.log(evenGreaterWriter("Toni Morrison"))
let myFruit="orange",
mySandwich = "just some bread!",
myBar="weird vegan bar",
myCookie="ginger",
myCandy="pulparindo",
mySnacks=["cliff bar", "cliff bar", "kind bar"];
function eat (food) {
console.log("Umm, that was a delicious " + food + ".");
}
eat (myFruit);
//eat (myBar);
//eat (myCookie);
//eat (myCandy);
myLunch = {};
myLunch = {
fruit: "orange",
sandwich: mySandwich
};
myLunch["snacks"]=mySnacks;
myLunch;
myLunch = {
fruit: "orange",
sandwich: mySandwich
};
myLunch["snacks"]=mySnacks;
function eatLunch (someLunch) {
let output = "";
output += "I'll start with my " + someLunch["fruit"] + "\n";
output += "Next I want to eat " + someLunch.sandwich + "\n";
for (snack of someLunch.snacks) {
output += " I love my " + snack + "\n";
}
return output;
}
eatLunch(myLunch);
myLunch = {
fruit: "orange",
sandwich: "just some bred!",
bar: "weird vegan bar",
snacks: ["cliff bar", "cliff bar", "kind bar"],
dessert: {cookie: "ginger", candy: "pulparindo"}
};
console.log("I'm finally ready for my " + myLunch.dessert.cookie);
function eatLunch (someLunch) {
let output = "";
output += "I'll start with my " + someLunch["fruit"] + "\n";
output += "Next I want to eat " + someLunch.sandwich + "\n";
for (snack of someLunch.snacks) {
output += " I love my " + snack + "\n";
}
return output;
}
myLunch = {
fruit: "orange",
sandwich: "just some bred!",
bar: "weird vegan bar",
snacks: ["cliff bar", "cliff bar", "kind bar"],
dessert: {cookie: "ginger", candy: "pulparindo"},
eatMe: function() {
let output = "";
output += "I'll start with my " + this["fruit"] + "\n";
output += "Next I want to eat " + this.sandwich + "\n";
for (snack of this.snacks) {
output += " I love my " + snack + "\n";
}
return output;
}
};
myLunch.eatMe;
myLunch.eatMe();
let greatWar = {
name: "The First World War",
start: 1914,
end: 1918,
badGuys: ["Germany", "Austria-Hungary", "Ottoman Empire"],
goodGuys: ["All our Friends"],
scale: "Catastrophe"
}
// console.log(greatWar["name"] + " lasted for " + (greatWar.end - greatWar.start) + " years.");
greatWar = {
name: "The First World War",
start: 1914,
end: 1918,
badGuys: ["Germany", "Austria-Hungary", "Ottoman Empire"],
goodGuys: ["All our Friends"],
scale: "Catastrophe",
battles: [
{ name: "The Battle of Vimy Ridge",
year: 1915,
casualties: 34000
}]
}
console.log(greatWar["name"] + " lasted for " + (greatWar.end - greatWar.start) + " \ years.");
console.log(greatWar.battles[0].name + " was ferocious and horrific. There were " + greatWar.battles[0].casualties + " casualties.");
goodGuys = ["CA", "UK", "US"];
badGuys=["DE", "AH"];
people = [
{name: "Kaiser Wilhelm",
nat: "DE"},
{name:"Winston Churchill",
nat: "UK"}];
for (person of people) {
if (badGuys.includes(person.nat)) {
//console.log(person.name + ": BOOOOOOOOOOOO!!!");
}
};
for (person of people) {
if (badGuys.includes(person.nat)) {
console.log(person.name + ": BOOOOOOOOOOOO!!!");
} else {
console.log(person.name + ": YAAAAYYY!!!");
}
};