-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathlevel2.js
346 lines (196 loc) · 7.65 KB
/
level2.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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Level 2
/*
Introduction
============
Welcome to level 2! It is time to interact with HTML.
Hyper Text Markup Language (HTML) files are the backbone of the internet.
Every website you visit is loaded first as an HTML file.
We won't talk too much about HTML today. What you need to know is that HTML
files function as a sort of skeleton for your page. Our index.html file
combines all the files from this project together so that you can open them
in your web browser.
This project has a file called index.html. At the bottom of index.html you
will see the following tag:
<script src="js/level1.js"></script>
That is how you have been running the level1.js file in a browser. Now change
level1.js to point to this file — level2.js.
Now you are ready to start!
*/
/*
Arrays
======
An array is an ordered list of values. It can keep any number of values
inside. And also any type of values — Numbers, Strings, Objects, even
other Arrays.
Example:
let animals = ['cat', 'dog', 'horse'];
let randomThings = [2, 'book', 'today is Saturday', 10];
let numbers = [1, 2, 8, 19];
*/
// TODO: Create your own array, named favouriteFood, and write in a couple of
// things you like.
/*
Array Length
============
We can easily check how many items we have in our array with a property:
'.length'
Example:
let randomThings = [2, 'book', 'today is Saturday', 10];
console.log(randomThings.length); // We will get 4.
*/
// TODO: Check how many values you have in your array favouriteFood.
// console.log the result.
/*
Array Usage
===========
It's useful to keep all of these values in one place. But what if we want
to use only 1 item from the array?
We can get them out using 'bracket notation'. Thanks to a guy named Edsger
Dijkstra*, array indices start counting from 0. The usage looks like this.
Example:
let randomThings = [2, 'book', 'today is Saturday', 10];
let firstItem = randomThings[0];
let secondItem = randomThings[1]; and so on
* https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html
*/
// TODO: Get the third element from your array favouriteFood and console.log it.
/*
Changing Values in Arrays
=========================
We also can replace values inside of the arrays by assigning a new value to
a specific index.
Example:
let animals = ['cat', 'dog', 'horse'];
// let's replace 'dog' with 'fish'
animals[1] = 'fish';
// now our animals array will be ['cat', 'fish', 'horse']
*/
// TODO: Take your array favouriteFood and replace the first value
// with anything else.
// TODO: console.log the whole array to check.
// TIP: Don't forget, index positions start from 0!
/*
Array.push()
============
If you want to add new values to an existing array you can use the method
'.push()'. Push will add a new value to the end of the array.
Example:
let animals = ['cat', 'dog', 'horse'];
animals.push('rabbit');
// animals will be ['cat', 'dog', 'horse','rabbit']
*/
// TODO: Let's extend your list of favouriteFood and add one more value to it.
// TODO: console.log the whole array to check.
/*
Note about constant Arrays
==========================
An array is what is known as a "reference type". What this means is that
even if an array is declared (created) using 'const', the values *inside* that
array can still be changed; only the array itself cannot be overwritten.
Example:
const animals = ['cat', 'dog', 'horse'];
// These are both legal, and works the same way as when animals is a variable:
animals[1] = 'fish';
animals.push('rabbit');
// This is illegal, and will return an error the same as changing any constant:
animals = [ 'mouse', 'elephant' ];
*/
// TODO: Try creating an array as a constant and modifying the values in it.
// TODO: See what happens if you add something with .push(), change something
// with bracket notation (array[1]) and
// assigning a new whole new array to the constant
/*
Loops
=====
People have always been lazy, but sometimes it moves progress forward! We
don't like to repeat the same boring actions again and again, so we look
for ways to avoid it.
Programming is the same. For example, if I want to print 'JavaScript is
awesome!' 10 times, what are my options? Of course, I can type ten lines of
code repeating the same instruction, but I can also tell the computer to
repeat it instead of me writing it 10 times!
For this we have loops.
Each loop should have three main things:
- a starting point
- a condition (finishing point)
- a counter (a step)
If you miss one of these, you can get into an infinite loop!
Let's look into different looping structures.
*/
/*
While Loops
===========
'While' loop will do an action forever until some condition is met.
Example:
// starting point
let number = 0;
while (number < 10) {
// 'less than 10' is a condition (finishing point)
console.log('JavaScript is awesome!');
number = number + 1;
// + 1 is a counter/size of the step
}
*/
// TODO: Using a 'while loop', tell your computer to log the numbers from
// ten to one.
/*
For Loops
=========
For loops are very similar to the 'while loop'. In a for loop, you
declare a counter in the statement.
Example:
let i;
for (i = 0; i <= 5; i = i + 1) { // (starting point; condition; step)
console.log('Purr');
}
*/
// TODO: Log every 3rd number from three to 22 using a 'for loop'.
/*
Iterating Through Arrays
========================
Now we know about loops, I want to use each value from my animal list
and express my love for each. How shall I do it?
We can use a 'for loop' to iterate through our array and get each value
from it.
Note: i++ is another way of writing i = i + 1.
Example:
const animals = ['cats', 'dogs', 'horses'];
for(let i = 0; i < animals.length; i++){
console.log('I love ' + animals[i]);
}
*/
// TODO: Try it out with your favouriteFood array.
/*
Loops and Logic
===============
Let's bring loops together with the if/else statements we learned in
level 1, and make something interesting.
Let's count from 10 to 0 and log all the numbers. But, when we get to the
middle (5) print 'Woohoo, we are in the middle!'.
Example:
for (let i = 10; i >= 0; i = i - 1) {
if (i === 5) {
console.log('WooHoo, we are in the middle!');
} else {
console.log(i);
}
}
*/
// TODO: Time has come for a classic exercise — 'FizzBuzz'.
// Count from 1 to 50 and print the numbers out:
//
// * If a number is a multiple of three, print 'Fizz'.
// * If it's a multiple of 5, print 'Buzz'.
// * If it's a multiple of 3 and 5, print 'FizzBuzz'.
// * For everything else, print the number itself.
// P.S. You may wish to use arithmetic operator remainder (%):
// It calculates the remainder when dividing.
//
// 10 % 3 = 1 — in 10 we have 3*3 + 1
// 16 % 4 = 0 — in 16 we have 4*4
// 19 % 4 = 3 — in 19 we have 4*4 + 3 etc
////////////////////////////////////////////////////////////////////////////
// Congratulations! You have finished Level 2 of JavaScript Basics! //
// Stand up, stretch your legs, and celebrate your achievement. //
// The next step will be following up the instructions in level3.js file. //
////////////////////////////////////////////////////////////////////////////