Skip to content

Commit 7f570e6

Browse files
tweak to chapter 3 wrong section + tweak to code blocks
1 parent 5eec9d8 commit 7f570e6

22 files changed

+158
-180
lines changed

ebook/02_arrow_functions.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const greeting = name => `hello ${name}`;
4747

4848
Look at a side by side comparison with an old ES5 Function:
4949

50-
```js
50+
```javascript
5151
const oldFunction = function(name){
5252
return `hello ${name}`
5353
}
@@ -58,7 +58,7 @@ const arrowFunction = name => `hello ${name}`;
5858
Both functions achieve the same result, but the new syntax allows you to be more concise.
5959
Beware! Readability is more important than conciseness so you might want to write your function like this if you are working in a team and not everybody is totally up-to-date with ES6.
6060

61-
```js
61+
```javascript
6262
const arrowFunction = (name) => {
6363
return `hello ${name}`;
6464
}
@@ -175,7 +175,7 @@ The `arguments object` is an array-like object that we can access from inside fu
175175

176176
A quick example:
177177

178-
```js
178+
```javascript
179179
function example(){
180180
console.log(arguments[0])
181181
}
@@ -222,7 +222,7 @@ showWinner("Usain Bolt", "Justin Gatlin", "Asafa Powell" )
222222

223223
Example with **function**:
224224

225-
```js
225+
```javascript
226226
const showWinner = function() {
227227
const winner = arguments[0];
228228
return `${winner} was the winner`

ebook/03_default_function_arguments.md

+8-30
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Prior to ES6, setting default values to function arguments was not so easy. Let's look at an example:
44

5-
``` js
5+
```javascript
66
function getLocation(city,country,continent){
77
if(typeof country === 'undefined'){
88
country = 'Italy'
@@ -26,7 +26,7 @@ When calling `getLocation('Milan')` the second and third parameter (country and
2626

2727
But what if we want our default value to be at the beginning and not at the end of our list of arguments?
2828

29-
``` js
29+
```javascript
3030
function getLocation(continent,country,city){
3131
if(typeof country === 'undefined'){
3232
country = 'Italy'
@@ -52,7 +52,7 @@ If we want to replace any of the first arguments with our default we need to pas
5252

5353
ES6 makes it very easy to set default function arguments. Let's look at an example:
5454

55-
``` js
55+
```javascript
5656
function calculatePrice(total, tax = 0.1, tip = 0.05){
5757
// When no value is given for tax or tip, the default 0.1 and 0.05 will be used
5858
return total + (total * tax) + (total * tip);
@@ -61,14 +61,14 @@ return total + (total * tax) + (total * tip);
6161

6262
What if we don't want to pass the parameter at all, like this:
6363

64-
``` js
64+
```javascript
6565
// The 0.15 will be bound to the second argument, tax even if in our intention it was to set 0.15 as the tip
6666
calculatePrice(100, 0.15)
6767
```
6868

6969
We can solve by doing this:
7070

71-
``` js
71+
```javascript
7272
// In this case 0.15 will be bound to the tip
7373
calculatePrice(100, undefined, 0.15)
7474
```
@@ -77,7 +77,7 @@ It works, but it's not very nice, how to improve it?
7777

7878
With **destructuring** we can write this:
7979

80-
``` js
80+
```javascript
8181
function calculatePrice({
8282
total = 0,
8383
tax = 0.1,
@@ -94,7 +94,7 @@ In the example above the default value for *tip* was 0.05 and we overwrote it wi
9494

9595
Notice this detail:
9696

97-
``` js
97+
```javascript
9898
{
9999
total = 0,
100100
tax = 0.1,
@@ -110,7 +110,7 @@ Cannot destructure property `total` of 'undefined' or 'null'.
110110

111111
By writing `= {}` we default our argument to an `Object` and no matter what argument we pass in the function, it will be an `Object`:
112112
113-
```js
113+
```javascript
114114
calculatePrice({});
115115
// 0
116116
calculatePrice();
@@ -122,25 +122,3 @@ calculatePrice(undefined)
122122
No matter what we passed, the argument was defaulted to an `Object` which had three default properties of total, tax and tip.
123123

124124
Don't worry about destructuring, we will talk about it in Chapter 10.
125-
126-
Note: We now don't need to construct object and equate to an empty object. Alternative to above we can construct a function as below
127-
128-
``` js
129-
function calculatePrice({
130-
total = 0,
131-
tax = 0.1,
132-
tip = 0.05}){
133-
return total + (total * tax) + (total * tip);
134-
}
135-
```
136-
137-
and the below code would work normal
138-
139-
``` js
140-
calculatePrice({});
141-
// 0
142-
calculatePrice();
143-
// 0
144-
calculatePrice(undefined)
145-
// 0
146-
```

ebook/04_template_literals.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ how are you?`;
7676

7777
It's very easy to nest a template inside another one, like this:
7878

79-
``` js
79+
```javascript
8080
const markup = `
8181
<ul>
8282
${people.map(person => `<li> ${person.name}</li>`)}
@@ -92,7 +92,7 @@ We can easily add some logic inside our template string by using a ternary opera
9292

9393
The syntax for a ternary operator looks like this:
9494

95-
```js
95+
```javascript
9696
const isDiscounted = false
9797

9898
return isDiscounted ? "$10" : "$20"
@@ -101,7 +101,7 @@ return isDiscounted ? "$10" : "$20"
101101

102102
If the condition before the `?` can be converted to `true` then the first value is returned, else it's the value after the `:` that gets returned.
103103

104-
``` js
104+
```javascript
105105
// create an artist with name and age
106106
const artist = {
107107
name: "Bon Jovi",
@@ -136,7 +136,7 @@ const artist = {
136136

137137
Similarly to the example above, if we want to, we can pass a function inside a template literal.
138138

139-
``` js
139+
```javascript
140140
const groceries = {
141141
meat: "pork chop",
142142
veggie: "salad",
@@ -184,7 +184,7 @@ By tagging a function to a template literal we can run the template literal thro
184184

185185
The way it works is very simple: you just take the name of your function and put it in front of the template that you want to run it against.
186186

187-
```js
187+
```javascript
188188
let person = "Alberto";
189189
let age = 25;
190190

@@ -208,7 +208,7 @@ We captured the value of the variable age and used a ternary operator to decide
208208
In our example our string is divided in 3 pieces: `${person}`, `is a` and `${age}`.
209209
We use array notation to access the string in the middle like this:
210210

211-
```js
211+
```javascript
212212
let str = strings[1];
213213
```
214214

ebook/05_additional_string_methods.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ There are many methods that we can use against strings. Here's a list of a few o
66

77
Gets the position of the first occurrence of the specified value in a string.
88

9-
```js
9+
```javascript
1010
const str = "this is a short sentence";
1111
str.indexOf("short");
1212
// Output: 10
@@ -16,7 +16,7 @@ str.indexOf("short");
1616

1717
Pulls a specified part of a string as a new string.
1818

19-
```js
19+
```javascript
2020
const str = "pizza, orange, cereals"
2121
str.slice(0, 5);
2222
// Output: "pizza"
@@ -26,7 +26,7 @@ str.slice(0, 5);
2626

2727
Turns all characters of a string to uppercase.
2828

29-
```js
29+
```javascript
3030
const str = "i ate an apple"
3131
str.toUpperCase()
3232
// Output: "I ATE AN APPLE"
@@ -36,7 +36,7 @@ str.toUpperCase()
3636

3737
Turns all characters of a string to lowercase.
3838

39-
```JS
39+
```javascript
4040
const str = "I ATE AN APPLE"
4141
str.toLowerCase()
4242
// Output: "i ate an apple"
@@ -61,7 +61,7 @@ ES6 introduced 4 new string methods:
6161

6262
This new method will check if the string starts with the value we pass in:
6363

64-
```js
64+
```javascript
6565
const code = "ABCDEFG";
6666

6767
code.startsWith("ABB");
@@ -74,7 +74,7 @@ code.startsWith("ABC");
7474

7575
We can pass an additional parameter, which is the starting point where the method will begin checking.
7676

77-
``` js
77+
```javascript
7878
const code = "ABCDEFGHI"
7979

8080
code.startsWith("DEF",3);
@@ -87,7 +87,7 @@ code.startsWith("DEF",3);
8787

8888
Similarly to `startsWith()` this new method will check if the string ends with the value we pass in:
8989

90-
```js
90+
```javascript
9191
const code = "ABCDEF";
9292

9393
code.endsWith("DDD");
@@ -101,7 +101,7 @@ code.endsWith("DEF");
101101

102102
We can pass an additional parameter, which is the number of digits we want to consider when checking the ending.
103103

104-
``` js
104+
```javascript
105105
const code = "ABCDEFGHI"
106106

107107
code.endsWith("EF", 6);
@@ -114,7 +114,7 @@ code.endsWith("EF", 6);
114114

115115
This method will check if our string includes the value we pass in.
116116

117-
```js
117+
```javascript
118118
const code = "ABCDEF"
119119

120120
code.includes("ABB");
@@ -131,7 +131,7 @@ code.includes("CDE");
131131

132132
As the name suggests, this new method will repeat what we pass in.
133133

134-
``` js
134+
```javascript
135135
let hello = "Hi";
136136
console.log(hello.repeat(10));
137137
// "HiHiHiHiHiHiHiHiHiHi"

ebook/06_destructuring.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Let's start with **destructuring objects** first.
1212

1313
To create variables from an object we used to do this:
1414

15-
```js
15+
```javascript
1616
var person = {
1717
first: "Alberto",
1818
last: "Montalesi"
@@ -24,7 +24,7 @@ var last = person.last;
2424

2525
In ES6 we can now write this:
2626

27-
```js
27+
```javascript
2828
const person = {
2929
first: "Alberto",
3030
last: "Montalesi"
@@ -37,7 +37,7 @@ Since our `const` have the same name as the properties we want to grab, we don't
3737

3838
The same applies even when we have nested data, such as what we could get from an API.
3939

40-
```js
40+
```javascript
4141
const person = {
4242
name: "Alberto",
4343
last: "Montalesi",
@@ -54,14 +54,14 @@ const { facebook } = person.links.social;
5454

5555
We are not limited to name our variable the same as the property of the object, we can also rename it like this:
5656

57-
```js
57+
```javascript
5858
const { facebook:fb } = person.links.social;
5959
// it will look for the property person.links.social.facebook and name the variable fb
6060
```
6161

6262
We can also pass in **default values** like this:
6363

64-
```js
64+
```javascript
6565
const { facebook:fb = "https://www.facebook.com"} = person.links.social;
6666
// we renamed the variable to *fb* and we also set a default value to it
6767
```
@@ -72,14 +72,14 @@ const { facebook:fb = "https://www.facebook.com"} = person.links.social;
7272

7373
The first difference we notice when **destructuring arrays** is that we are going to use `[]` and not `{}`.
7474

75-
```js
75+
```javascript
7676
const person = ["Alberto","Montalesi",25];
7777
const [name,surname,age] = person;
7878
```
7979

8080
What if the number of variables that we create is less than the elements in the array?
8181

82-
```js
82+
```javascript
8383
const person = ["Alberto","Montalesi",25];
8484
// we leave out age, we don't want it
8585
const [name,surname] = person;
@@ -90,7 +90,7 @@ console.log(name,surname);
9090

9191
Let's say we want to grab all the other values remaining, we can use the **rest operator**:
9292

93-
```js
93+
```javascript
9494
const person = ["Alberto", "Montalesi", "pizza", "ice cream", "cheese cake"];
9595
// we use the **rest operator** to grab all the remaining values
9696
const [name,surname,...food] = person ;
@@ -108,7 +108,7 @@ The `...` is the syntax for the `rest operator`
108108

109109
The destructuring assignment makes it **extremely easy** to swap variables, just look at this example:
110110

111-
```js
111+
```javascript
112112
let hungry = "yes";
113113
let full = "no";
114114
// after we eat we don't feel hungry anymore, we feel full, let's swap the values
@@ -124,7 +124,7 @@ It can't get easier than this to swap values.
124124

125125
## Destructuring functions
126126

127-
```js
127+
```javascript
128128
function totalBill({ total, tax = 0.1 }) {
129129
return total + (total * tax);
130130
}

0 commit comments

Comments
 (0)