-
Notifications
You must be signed in to change notification settings - Fork 0
/
ES6.js
159 lines (133 loc) · 4.68 KB
/
ES6.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
// ================== Object destructuring assignment of function parameters ===================
// ????????????
const profile = {
name: "John",
age: 34,
nationality: "American",
location: "Europe"
}
// --------- From fCC. Doesn't work as is.... -------------
const profileUpdate = (profileData) => {
const { name, age, nationality, location } = profileData;
}
// ------ The same as -----------
// --------- Neither does this... ------------
const profileUpdate = ({ name, age, nationality, location }) => {
}
// ------- Maybe the same as... ---------
const profileUpdate = ({ name, age, location }) => `My name is ${name}, I'm ${age} years old and I live in ${location}.`;
// ------------ Or ---------------
const profileUpdate = ({ name, age, location }) => [name, age, location];
console.log(profileUpdate(profile));
// ----------- Another -----------------
const stats = {
max: 56.78,
standard_deviation: 4.34,
median: 34.54,
mode: 23.87,
min: -0.75,
average: 35.85
};
// Only change code below this line
// const half = (nums) => (nums.max + nums.min) / 2.0; // Works
// --------- the same as -------------
const half = ({ max, min }) => (max + min) / 2.0; // Also works
// Only change code above this line
console.log(half(stats));
// ================== Template literals (instead of concatenation) ===================
// First, use backticks (`), not quotes (' or "), to wrap the string. Second, notice that the string is multi-line, both in the code and the output. This saves inserting \n within strings. The ${variable} syntax used is a placeholder. To add variables to strings, you just drop the variable in a template string and wrap it with ${ and }. Similarly, you can include other expressions in your string literal, for example ${a + b}.
const person = {
name: "Zodiac Hasbro",
age: 56
};
const greeting = `Hello, my name is ${person.name}!
I am ${person.age} years old.`;
console.log(greeting); // Hello, my name is Zodiac Hasbro!
// I am 56 years old.
// ------------ And -------------
const result = {
success: ["max-length", "no-amd", "prefer-arrow-functions"],
failure: ["no-var", "var-on-top", "linebreak"],
skipped: ["no-extra-semi", "no-dup-keys"]
};
function makeList(arr) {
// Only change code below this line
const failureItems = [];
for (let i = 0; i < arr.length; i++) {
failureItems.push(`<li class="text-warning">${arr[i]}</li>`);
// the same as this one below which I don't know how .map works...
// const failureItems = arr.map(item => `<li class="text-warning">${item}</li>`);
}
// Only change code above this line
return failureItems;
}
const failuresList = makeList(result.failure);
console.log(failuresList)
// ------------------------
const createPerson = (name, age, gender) => {
return {
name, age, gender
};
};
// Or simpler
const createPerson = (name, age, gender) => ({ name, age, gender });
console.log(createPerson("Zodiac Hasbro", 56, "male")); // { name: 'Zodiac Hasbro', age: 56, gender: 'male' }
// -------------------------------
const bicycle = {
gear: 2,
setGear(newGear) {
this.gear = newGear;
}
};
// Only change code above this line
bicycle.setGear(3);
console.log(bicycle.gear);
// ================== Class and constructor ===================
// Only change code below this line
class Vegetable {
constructor(name) {
this.name = name;
}
}
// Only change code above this line
const carrot = new Vegetable('carrot');
console.log(carrot.name); // Should display 'carrot'
// ----------------- another --------------
// Only change code below this line
class Thermostat {
constructor(fahrTemp) {
this._fahrTemp = fahrTemp;
}
// getter
get temperature() {
return (this._fahrTemp - 32) * 5 / 9;
}
// setter
set temperature(celTemp) {
this._fahrTemp = celTemp * 9 / 5 + 32;
}
}
// Only change code above this line
const thermos = new Thermostat(76); // Setting in Fahrenheit scale
console.log(thermos); // Thermostat { _fahrTemp: 76 }
let temp = thermos.temperature; // 24.44 in Celsius
console.log(temp); // 24.444444444444443
thermos.temperature = 26;
temp = thermos.temperature; // 26 in Celsius
console.log(temp); // 26
// ---------------------------------------------
const makeServerRequest = new Promise((resolve, reject) => {
// responseFromServer is set to represent a response from a server
let responseFromServer = false;
if (responseFromServer) {
resolve("We got the data");
} else {
reject("Data not received");
}
});
makeServerRequest.then(result => {
console.log(result);
});
makeServerRequest.catch(error => {
console.log(error);
});