You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: js/lesson2/tutorial.md
+26-9Lines changed: 26 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,7 +42,7 @@ var person = {
42
42
console.log('Hello '+person.first_name+'!');
43
43
```
44
44
45
-
We're going to introduce one more important type: **booleans**. A
45
+
We are now going to introduce one more important type: **booleans**. A
46
46
boolean can only be `true` or `false`, for example:
47
47
48
48
```js
@@ -84,7 +84,7 @@ console.log('Division: x / y = ' + division); // Division: x / y = 2
84
84
85
85
#### More maths
86
86
87
-
Other basic but useful math operators are `%`, `**`, `++` and `--`:
87
+
Other useful math operators are `%`, `**`, `++` and `--`:
88
88
89
89
> The modulus `%` operator returns the remainder when dividing one operand by another.
90
90
@@ -137,7 +137,7 @@ console.log('Apples and oranges are different: ' + notEqual);
137
137
```
138
138
139
139
> You may also see `==` and `!=`, these are similar but have some quirks so it's generally recommended to avoid them.
140
-
> You can just use `===` and `!==` and they will
140
+
> You can simply use `===` and `!==` and they will
141
141
> always do what you expect.
142
142
143
143
The `>` and `<` operators are "greater than" and "less than". You can
@@ -155,6 +155,7 @@ var lessStudents = students < pizzas;
155
155
console.log('Are there fewer students than pizzas?'+ lessStudents);
156
156
157
157
```
158
+
> Play around with changing the `coaches`, `students` and `pizzas` variable numbers to familiarise yourself with it more.
158
159
159
160
You can also combine operators.
160
161
@@ -177,6 +178,8 @@ if (codebarIsAwesome) {
177
178
}
178
179
```
179
180
181
+
> Change the `codebarIsAwesome` variable to false, what happens now?
182
+
180
183
You can use an expression inside an if statement.
181
184
182
185
```js
@@ -205,6 +208,18 @@ if (totalPeople > pizzas) {
205
208
}
206
209
```
207
210
211
+
#### Ternaries - Additional learning
212
+
213
+
In the latest versions of JavaScript you may see if/else statements written slighlty differently. We believe teaching the above way first helps cement how they work, however if you're working with a code base that uses the latest JavaScript you will see it written this way.
214
+
215
+
The condition followed by a question mark (?), then an expression to execute if the condition is truthy followed by a colon (:) and finally the expression to execute if the condition is falsy. For example:
216
+
217
+
```js
218
+
totalPeople > pizzas
219
+
?console.log('We have more people than pizzas.')
220
+
:console.log('We have waaay too much pizza. That can never be bad! :)')
221
+
```
222
+
208
223
## Loops
209
224
210
225
A loop is used to execute a set of statements repeatedly until a condition is met.
@@ -219,9 +234,9 @@ while (test) {
219
234
}
220
235
```
221
236
222
-
When the while loop starts, the test is checked. If it is false, then the while loop is skipped. If the test is true, then just like an `if` block the statements in the curly braces are executed.
237
+
When the while loop starts, the test is checked. If it is false, then the while loop is skipped. If the test is true, then like an `if` block the statements in the curly braces are executed.
223
238
224
-
The difference is in what happens after the statements in the block. With an `if`, everything is finished and the statements below the if block are executed. With a `while`, we go back up to the test. If the test is still true, the statements in the block are executed again, and so on until the test is false. This is why we call it a loop.
239
+
The difference is what happens after the statements in the block. With an `if`, everything is finished and the statements below the if block are executed. With a `while`, we go back up to the test. If the test is still true, the statements in the block are executed again, and so on until the test is false. This is why we call it a loop.
225
240
226
241
For example, if we wanted to set a timer on an online ticket outlet, we could count the timer down and while it hasn't reached zero, the option to buy the ticket could still be available.
227
242
@@ -263,7 +278,7 @@ for (/* before loop starts */; /* test before each iteration */; /* after each i
263
278
264
279
The stuff in the round brackets is split into three parts by `;`. The first part is used once, before the loop begins. It's a good place to set an initial value for a counter (like `x = 1` in the `while` loop example).
265
280
266
-
The second part is a test, and just like in the `while` loop it is checked before each iteration.
281
+
The second part is a test, and like in the `while` loop it is checked before each iteration.
267
282
268
283
The third part is executed after each loop iteration. It's useful for incrementing the loop counter.
> Another way to write the for loop is `for (var i = 1; i <= 10; i++)`. The `i++` is a short way of writing "increase i by one".
285
300
286
-
Even though `while` loops are more simple than `for` loops, it is more common to see `for` loops. This is because loops are often used to do something with arrays, which are introduced in the next section.
301
+
Even though `while` loops are simpler than `for` loops, it is more common to see `for` loops. This is because loops are often used to do something with arrays, which are introduced in the next section.
287
302
288
303
## Arrays
289
304
290
305
An array is a simple *data structure*. It can hold a list of elements of the same or different types (e.g. strings, numbers, booleans). In an array each element can be accessed using the **index**.
291
306
292
-
It may be a bit confusing, but the first index of an array is 0, not 1.
307
+
Confusingly, the first index of an array is 0, not 1.
293
308
294
309
To understand this better, let's create an array of strings.
295
310
@@ -304,6 +319,8 @@ To retrieve an item from the array, we use **square bracket notation**
304
319
305
320
To get the first item `animals[0]`, the second `animals[1]` etc.
306
321
322
+
> Create an array that includes 5 names and then access the 3rd item in the array? (don't forget to access the third item in the array you won't be doing [3])
323
+
307
324
### Properties - `length`
308
325
309
326
The `length` property returns the size of the array
0 commit comments