|
1 |
| -//Level2 |
| 1 | +// Level2 |
2 | 2 |
|
3 | 3 | /*
|
4 |
| - Now, when we start level2, it is time to interact with html file. HTML files is a structure |
5 |
| - of our web-page - a skeleton. It combines all files from the project together, that is how |
6 |
| - it know what to do. If you check on our index.html file on the bottom you will see tag |
7 |
| - <script src="js/level1js"></script> - that is where we joined our first js file. But now |
8 |
| - change level1.js file on our current one - level2.js. |
9 |
| - Now you are ready to start! |
| 4 | + Introduction |
| 5 | + ============ |
| 6 | +
|
| 7 | + Welcome to level 2! It is time to interact with HTML. |
| 8 | +
|
| 9 | + Hyper Text Markup Language (HTML) files are the backbone of the internet. |
| 10 | + Every website you visit is loaded first as an HTML file. |
| 11 | +
|
| 12 | + We won't talk too much about HTML today. What you need to know is that HTML |
| 13 | + files function as a sort of skeleton for your page. Our index.html file |
| 14 | + combines all the files from this project together so that you can open them |
| 15 | + in your web browser. |
| 16 | +
|
| 17 | + This project has a file called index.html. At the bottom of index.html you |
| 18 | + will see the following tag: |
| 19 | +
|
| 20 | + <script src="js/level1.js"></script> |
| 21 | +
|
| 22 | + That is how you have been running the level1.js file. Now change level1.js |
| 23 | + to this file — level2.js. |
| 24 | +
|
| 25 | + Now you are ready to start! |
10 | 26 | */
|
11 | 27 |
|
12 |
| -/* Arrays |
13 |
| - It is an ordered list of values. It can keep any number of values inside. And also |
14 |
| - any type of values - numbers, strings, objects. |
| 28 | +/* |
| 29 | + Arrays |
| 30 | + ====== |
| 31 | +
|
| 32 | + An array is an ordered list of values. It can keep any number of values |
| 33 | + inside. And also any type of values — numbers, strings, objects. |
| 34 | +
|
15 | 35 | Example:
|
| 36 | +
|
16 | 37 | var animals = ['cat', 'dog', 'horse];
|
17 | 38 | var randomThings = [2, 'book', 'today is Saturday', 10];
|
18 | 39 | var numbers = [1, 2, 8, 19];
|
19 | 40 | */
|
20 | 41 |
|
21 |
| -//TODO: create your own array named favouriteFood and write in couple of things you like. |
| 42 | +// TODO: Create your own array, named favouriteFood, and write in a couple of |
| 43 | +// things you like. |
| 44 | + |
22 | 45 |
|
23 | 46 |
|
24 | 47 |
|
25 | 48 |
|
| 49 | +/* |
| 50 | + Array Length |
| 51 | + ============ |
| 52 | +
|
| 53 | + We can easily check how many items we have in our array with a property: |
| 54 | + '.length' |
26 | 55 |
|
27 |
| -/* Array length |
28 |
| - We can easily check how many items we have in our array with property '.length' |
29 | 56 | Example:
|
30 | 57 | var randomThings = [2, 'book', 'today is Saturday', 10];
|
31 |
| - console.log(randomThings.length); //we will get 4 |
| 58 | + console.log(randomThings.length); // we will get 4 |
32 | 59 | */
|
33 | 60 |
|
34 |
| -//TODO: check up on how many values you have in your array favouriteFood and console.log it |
| 61 | +// TODO: Check up on how many values you have in your array favouriteFood. |
| 62 | +// console.log the result. |
| 63 | + |
| 64 | + |
35 | 65 |
|
36 | 66 |
|
37 | 67 |
|
38 | 68 |
|
| 69 | +/* |
| 70 | + Array Usage |
| 71 | + =========== |
| 72 | +
|
| 73 | + It's useful to keep all of these values in one place. But what if we want |
| 74 | + to use only 1 item from the array? |
39 | 75 |
|
| 76 | + We can get them out using 'bracket notation'. Thanks to a guy named Edsger |
| 77 | + Dijkstra*, array indices start counting from 0. Usage looks like this. |
40 | 78 |
|
41 |
| -/* Arrays usage |
42 |
| - So, it is comfortable to keep all this values in one place, but what if we want to use |
43 |
| - only 1 item from the array, we can get them out using 'bracket notation'. |
44 |
| - As all counting in programming starts with 0, the first element in the array will have |
45 |
| - position 0 etc |
46 | 79 | Example:
|
| 80 | +
|
47 | 81 | var randomThings = [2, 'book', 'today is Saturday', 10];
|
48 | 82 | var firstItem = randomThings[0];
|
49 | 83 | var secondItem = randomThings[1]; and so on
|
| 84 | +
|
| 85 | + * https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html |
50 | 86 | */
|
51 | 87 |
|
52 |
| -//TODO: get 3rd element from your array favouriteFood and console.log it |
| 88 | +// TODO: Get third element from your array favouriteFood and console.log it. |
53 | 89 |
|
54 | 90 |
|
55 | 91 |
|
56 | 92 |
|
57 | 93 |
|
58 | 94 |
|
59 | 95 |
|
60 |
| -/* Changing values in arrays |
61 |
| - We also can replace values inside of the arrays by assigning specific item from |
62 |
| - the array to a new value. |
| 96 | +/* |
| 97 | + Changing Values in Arrays |
| 98 | + ========================= |
| 99 | +
|
| 100 | + We also can replace values inside of the arrays by assigning a new value to |
| 101 | + a specific index. |
| 102 | +
|
63 | 103 | Example:
|
| 104 | +
|
64 | 105 | var animals = ['cat', 'dog', 'horse'];
|
65 |
| - //let's replace 'dog' with 'fish' |
| 106 | +
|
| 107 | + // let's replace 'dog' with 'fish' |
66 | 108 | animals[1] = 'fish';
|
67 |
| - //now our animals array will be ['cat', 'fish', 'horse'] |
| 109 | +
|
| 110 | + // now our animals array will be ['cat', 'fish', 'horse'] |
68 | 111 | */
|
69 | 112 |
|
70 |
| -//TODO: take your array favouriteFood and replace first value with anything else. |
71 |
| -//console.log whole array to check |
72 |
| -// Dont forget, that index position starts with 0 |
| 113 | +// TODO: take your array favouriteFood and replace first value with anything |
| 114 | +// else. |
| 115 | +// console.log whole array to check |
| 116 | +// Dont forget, index positions start from 0! |
| 117 | + |
| 118 | + |
73 | 119 |
|
74 | 120 |
|
75 | 121 |
|
76 | 122 |
|
77 | 123 |
|
| 124 | +/* |
| 125 | + Array.push() |
| 126 | + ============ |
78 | 127 |
|
| 128 | + If you want to add new values to an existing array you can use the method |
| 129 | + '.push()'. Push will add a new value to the end of the array. |
79 | 130 |
|
80 |
| -/* Adding values to array |
81 |
| - If you want to add new value to array you can use property of array named '.push'. |
82 |
| - It will add value to the end of the array |
83 | 131 | Example:
|
| 132 | +
|
84 | 133 | var animals = ['cat', 'dog', 'horse'];
|
85 | 134 | animals.push('rabbit');
|
86 |
| - so now our array will be ['cat', 'dog', 'horse','rabbit'] |
| 135 | +
|
| 136 | + // animals will be ['cat', 'dog', 'horse','rabbit'] |
87 | 137 | */
|
88 | 138 |
|
89 |
| -//TODO: let's extend your list of favouriteFood and add one more value in it. |
90 |
| -//console.log list of your favouriteFood and check |
| 139 | +// TODO: let's extend your list of favouriteFood and add one more value in it. |
| 140 | +// console.log list of your favouriteFood and check |
91 | 141 |
|
92 | 142 |
|
93 | 143 |
|
|
96 | 146 |
|
97 | 147 |
|
98 | 148 |
|
99 |
| -/* Loops |
100 |
| - People always have been lazy and it was moving progress forward. We don't like to repeat |
101 |
| - same boring actions again and again, so we are looking for ways how to improve it. |
102 |
| - Programming is the same - for example, if I want to print 'JavaScript is awesome!' 10 |
103 |
| - times what are my options? Of course, I can print 10 lines of code repeating same |
104 |
| - phrase over and over again, but I also can tell computer to repeat it instead of me. |
| 149 | +/* |
| 150 | + Loops |
| 151 | + ===== |
| 152 | +
|
| 153 | + People have always been lazy, but sometimes it moves progress forward! We |
| 154 | + don't like to repeat the same boring actions again and again, so we look |
| 155 | + for ways to avoid it. |
| 156 | +
|
| 157 | + Programming is the same. For example, if I want to print 'JavaScript is |
| 158 | + awesome!' 10 times, what are my options? Of course, I can type ten lines of |
| 159 | + code repeating the same instruction, but I can also tell the computer to |
| 160 | + repeat it instead of me! |
| 161 | +
|
105 | 162 | For this we have loops.
|
106 |
| - Each loop should have 3 main things: |
| 163 | +
|
| 164 | + Each loop should have three main things: |
107 | 165 | - starting point
|
108 | 166 | - condition (finishing point)
|
109 | 167 | - counter (a step)
|
110 |
| - If you miss one of them you can get into infinite loop!!! |
111 | 168 |
|
112 |
| - Let's look into different looping structures |
| 169 | + If you miss one of these, you can get into an infinite loop! |
| 170 | +
|
| 171 | + Let's look into different looping structures. |
113 | 172 | */
|
114 | 173 |
|
115 | 174 |
|
116 |
| -/* While loops |
117 |
| - 'While' loop will do the action over and over again untill |
118 |
| - some condition will be met. |
119 |
| - For example: |
120 |
| - var number = 0; //starting point |
121 |
| - while (number < 10) { //10 is a condition/finishing point |
122 |
| - console.log('JavaScript is awesome!'); |
123 |
| - number = number + 1; // + 1 is a counter/size of the step |
124 |
| - } |
| 175 | +/* |
| 176 | + While Loops |
| 177 | + =========== |
| 178 | +
|
| 179 | + 'While' loop will do an action forever until some condition is met. |
| 180 | +
|
| 181 | + Example: |
| 182 | +
|
| 183 | + // starting point |
| 184 | + var number = 0; |
| 185 | +
|
| 186 | + while (number < 10) { |
| 187 | + // 'less than 10'' is a condition (finishing point) |
| 188 | +
|
| 189 | + console.log('JavaScript is awesome!'); |
| 190 | +
|
| 191 | + number = number + 1; |
| 192 | + // + 1 is a counter/size of the step |
| 193 | + } |
125 | 194 | */
|
126 | 195 |
|
127 |
| -//TODO: Using 'while loop' tell your computer to print you numbers from 10 to 1 |
| 196 | +// TODO: Using a 'while loop', tell your computer to log the numbers from |
| 197 | +// ten to one. |
128 | 198 |
|
129 | 199 |
|
130 | 200 |
|
|
133 | 203 |
|
134 | 204 |
|
135 | 205 |
|
136 |
| -/* For loops |
137 |
| - 'For loop' is very similar to 'while loop', but you declare a counter in the statement. |
138 |
| - For example: |
139 |
| - for(var i = 0; i <= 5; i++){ //(starting point, condition, step) |
| 206 | +/* |
| 207 | + For Loops |
| 208 | + ========= |
| 209 | +
|
| 210 | + 'For loop's are very similar to the 'while loop'. In a for loop, you |
| 211 | + declare a counter in the statement. |
| 212 | +
|
| 213 | + Example: |
| 214 | +
|
| 215 | + var i; |
| 216 | + for (i = 0; i <= 5; i = i + 1) { // (starting point; condition; step) |
140 | 217 | console.log('Purr');
|
141 | 218 | }
|
142 |
| - PS: i++ is a short from 'i = i + 1' |
143 | 219 | */
|
144 | 220 |
|
145 |
| -//TODO: now, let's print every 3rd number from 3 to 22 using 'for loop' |
| 221 | +// TODO: Log every 3rd number from three to 22 using a 'for loop'. |
| 222 | + |
146 | 223 |
|
147 | 224 |
|
148 | 225 |
|
149 | 226 |
|
150 | 227 |
|
151 | 228 |
|
| 229 | +/* |
| 230 | + Iterating Through Arrays |
| 231 | + ======================== |
| 232 | +
|
| 233 | + Now we know about loops. I want to use each value from my animal list |
| 234 | + and express my love for each. How shall I do it? |
| 235 | +
|
| 236 | + We can use a 'for loop' to iterate through our array and get each value |
| 237 | + from it. |
152 | 238 |
|
153 |
| -/* Iterating through arrays |
154 |
| - Now, when we know about loops I want to use each value from my animal |
155 |
| - list and say that I love all of those animals, how shall I do it? |
156 |
| - We can use 'for loop' to iterate through our array and get each value from it. |
157 | 239 | Example:
|
| 240 | +
|
158 | 241 | var animals = ['cats', 'dogs', 'horses];
|
159 | 242 | for(var i = 0; i < animals.length; i++){
|
160 |
| - console.log('I love ' + animals[i]); |
| 243 | + console.log('I love ' + animals[i]); |
161 | 244 | }
|
162 | 245 | */
|
163 | 246 |
|
164 |
| -//TODO: try it out with your list of favouriteFood that you created before. |
| 247 | +// TODO: Try it out with your favouriteFood array. |
165 | 248 |
|
166 | 249 |
|
167 | 250 |
|
|
173 | 256 |
|
174 | 257 |
|
175 | 258 |
|
176 |
| -/* Loops and Logic |
177 |
| - Now, when we know about if/else statements and loops let's combine them together |
178 |
| - and make something more interesting. Let's count from 10 to 0 and print all the |
179 |
| - numbers, but when we get to the middle(5) print ''WooHoo, we are in the middle'. |
| 259 | +/* |
| 260 | + Loops and Logic |
| 261 | + =============== |
| 262 | +
|
| 263 | + Let's bring loops together with the if/else statements we learned in |
| 264 | + level 1, and make something interesting. |
| 265 | +
|
| 266 | + Let's count from 10 to 0 and log all the numbers. But, when we get to the |
| 267 | + middle (5) print 'Woohoo, we are in the middle!'. |
| 268 | +
|
180 | 269 | Example:
|
181 |
| - for(var i = 10; i > 0; i--){ |
182 |
| - if(i == 5){ |
183 |
| - console.log('WooHoo, we are in the middle'); |
184 |
| - } else { |
185 |
| - console.log(i); |
186 |
| - } |
| 270 | +
|
| 271 | + for (var i = 10; i >= 0; i = i - 1) { |
| 272 | + if (i === 5) { |
| 273 | + console.log('WooHoo, we are in the middle!'); |
| 274 | + } else { |
| 275 | + console.log(i); |
| 276 | + } |
187 | 277 | }
|
188 | 278 | */
|
189 | 279 |
|
190 |
| -//TODO: Time has come for a classic exercise - 'FizzBuzz'. Count from 1 to 50 and print |
191 |
| -// numbers out. If number is a multiple of 3 print 'Fizz', if its a multiple of 5 print 'Buzz', if |
192 |
| -// it is multiple of 3 and 5 print 'FizzBuzz', for everthing else print the number itself. |
193 |
| -//PS: you can use arithmetic operator modulo (%) - it is remainder when dividing. |
194 |
| -//10 % 3 = 1 - so in 10 we have 3*3 + 1 |
195 |
| -//16 % 4 = 0 - as in 16 we have 4*4 |
196 |
| -//19 % 4 = 3 - as in 19 we have 4*4 + 3 etc |
| 280 | +// TODO: Time has come for a classic exercise — 'FizzBuzz'. |
| 281 | + |
| 282 | +// Count from 1 to 50 and print numbers out: |
| 283 | +// |
| 284 | +// * If a number is a multiple of three, print 'Fizz' |
| 285 | +// * If it's a multiple of 5, print 'Buzz' |
| 286 | +// * If it is multiple of 3 and 5, print 'FizzBuzz' |
| 287 | +// * For everything else, print the number itself. |
| 288 | + |
| 289 | +// P.S. You may wish to use arithmetic operator remainder (%): |
| 290 | +// It calculates the remainder when dividing. |
| 291 | +// |
| 292 | +// 10 % 3 = 1 — in 10 we have 3*3 + 1 |
| 293 | +// 16 % 4 = 0 — in 16 we have 4*4 |
| 294 | +// 19 % 4 = 3 — in 19 we have 4*4 + 3 etc |
197 | 295 |
|
198 | 296 |
|
199 | 297 |
|
|
210 | 308 |
|
211 | 309 |
|
212 | 310 | ////////////////////////////////////////////////////////////////////////
|
213 |
| -//Congratulations! You have finished Part 2 of JavaScript Basics! // |
| 311 | +// Congratulations! You have finished Part 2 of JavaScript Basics! // |
214 | 312 | // Stand up, stretch your legs, celebrate your achievement. //
|
215 |
| -//Next step will be following up the instructions in level3.js file. // |
| 313 | +// Next step will be following up the instructions in level3.js file. // |
216 | 314 | ////////////////////////////////////////////////////////////////////////
|
0 commit comments