Skip to content

Commit 62df107

Browse files
ajhyndmanButenkoT
authored andcommitted
Suggestions for Level 2 (#4)
* Suggestions for Level 2 * fixup! Suggestions for Level 2
1 parent ed3b88e commit 62df107

File tree

1 file changed

+183
-85
lines changed

1 file changed

+183
-85
lines changed

js/level2.js

+183-85
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,143 @@
1-
//Level2
1+
// Level2
22

33
/*
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!
1026
*/
1127

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+
1535
Example:
36+
1637
var animals = ['cat', 'dog', 'horse];
1738
var randomThings = [2, 'book', 'today is Saturday', 10];
1839
var numbers = [1, 2, 8, 19];
1940
*/
2041

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+
2245

2346

2447

2548

49+
/*
50+
Array Length
51+
============
52+
53+
We can easily check how many items we have in our array with a property:
54+
'.length'
2655
27-
/* Array length
28-
We can easily check how many items we have in our array with property '.length'
2956
Example:
3057
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
3259
*/
3360

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+
3565

3666

3767

3868

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?
3975
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.
4078
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
4679
Example:
80+
4781
var randomThings = [2, 'book', 'today is Saturday', 10];
4882
var firstItem = randomThings[0];
4983
var secondItem = randomThings[1]; and so on
84+
85+
* https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html
5086
*/
5187

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.
5389

5490

5591

5692

5793

5894

5995

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+
63103
Example:
104+
64105
var animals = ['cat', 'dog', 'horse'];
65-
//let's replace 'dog' with 'fish'
106+
107+
// let's replace 'dog' with 'fish'
66108
animals[1] = 'fish';
67-
//now our animals array will be ['cat', 'fish', 'horse']
109+
110+
// now our animals array will be ['cat', 'fish', 'horse']
68111
*/
69112

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+
73119

74120

75121

76122

77123

124+
/*
125+
Array.push()
126+
============
78127
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.
79130
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
83131
Example:
132+
84133
var animals = ['cat', 'dog', 'horse'];
85134
animals.push('rabbit');
86-
so now our array will be ['cat', 'dog', 'horse','rabbit']
135+
136+
// animals will be ['cat', 'dog', 'horse','rabbit']
87137
*/
88138

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
91141

92142

93143

@@ -96,35 +146,55 @@
96146

97147

98148

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+
105162
For this we have loops.
106-
Each loop should have 3 main things:
163+
164+
Each loop should have three main things:
107165
- starting point
108166
- condition (finishing point)
109167
- counter (a step)
110-
If you miss one of them you can get into infinite loop!!!
111168
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.
113172
*/
114173

115174

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+
}
125194
*/
126195

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.
128198

129199

130200

@@ -133,35 +203,48 @@
133203

134204

135205

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)
140217
console.log('Purr');
141218
}
142-
PS: i++ is a short from 'i = i + 1'
143219
*/
144220

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+
146223

147224

148225

149226

150227

151228

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.
152238
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.
157239
Example:
240+
158241
var animals = ['cats', 'dogs', 'horses];
159242
for(var i = 0; i < animals.length; i++){
160-
console.log('I love ' + animals[i]);
243+
console.log('I love ' + animals[i]);
161244
}
162245
*/
163246

164-
//TODO: try it out with your list of favouriteFood that you created before.
247+
// TODO: Try it out with your favouriteFood array.
165248

166249

167250

@@ -173,27 +256,42 @@
173256

174257

175258

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+
180269
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+
}
187277
}
188278
*/
189279

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
197295

198296

199297

@@ -210,7 +308,7 @@
210308

211309

212310
////////////////////////////////////////////////////////////////////////
213-
//Congratulations! You have finished Part 2 of JavaScript Basics! //
311+
// Congratulations! You have finished Part 2 of JavaScript Basics! //
214312
// 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. //
216314
////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)