-
Notifications
You must be signed in to change notification settings - Fork 0
/
destructuring.js
107 lines (79 loc) · 3.46 KB
/
destructuring.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
////////////////////Object Destructuring 1////////////////////
//What does the following code return/print?
let facts = {numberPlanets: 8, yearNeptuneDiscovered: 1846};
let {numberPlanets, yearNeptuneDiscovered} = facts;
//console.log(numberPlanets); // 8
//console.log(yearNeptuneDiscovered); // 1846
////////////////////Object Destructuring 2////////////////////
//What does the following code return/print?
let planetFacts = {
numPlanets: 8,
yearNeptuneDiscovered: 1846,
yearMarsDiscovered: 1659
};
let {numPlanets, ...discoveryYears} = planetFacts;
//console.log(discoveryYears); // {yearNeptuneDiscovered: 1846, yearMarsDiscovered: 1659};
////////////////////Object Destructuring 3//////////////////
//What does the following code return/print?
function getUserData({firstName, favoriteColor="green"}){
return `Your name is ${firstName} and you like ${favoriteColor}`;
}
getUserData({firstName: "Alejandro", favoriteColor: "purple"}) // 'Your name is Alejandro and you like purple';
getUserData({firstName: "Melissa"}) // 'Your name is Melissa and you like green';
getUserData({}) // 'Your name is undefined and you like green';
/////////////////////Array Destructuring 1//////////////////
//What does the following code return/print?
let [first, second, third] = ["Maya", "Marisa", "Chi"];
//console.log(first); // Maya
//console.log(second); // Marisa
//console.log(third); // Chi
////////////////////Array Destructuring 2//////////////////
//What does the following code return/print?
let [raindrops, whiskers, ...aFewOfMyFavoriteThings] = [
"Raindrops on roses",
"whiskers on kittens",
"Bright copper kettles",
"warm woolen mittens",
"Brown paper packages tied up with strings"
]
//console.log(raindrops); // Raindrops on roses
//console.log(whiskers); // whiskers on kittens
//console.log(aFewOfMyFavoriteThings); // ["Bright copper kettles", "warm woolen mittens", "Brown paper packages tied up with strings"]
/////////////////////Array Destructuring 3///////////////
//What does the following code return/print?
let numbers = [10, 20, 30];
[numbers[1], numbers[2]] = [numbers[2], numbers[1]]
//console.log(numbers) // [10, 30, 20]
//////////////////////////////## **ES2015 Refactoring**//////////////////////////////////////////////
//In this exercise, you’ll refactor some ES5 code into ES2015.
///////////ES5 Assigning Variables to Object Properties//////////////////
// var obj = {
// numbers: {
// a: 1,
// b: 2
// }
// };
// var a = obj.numbers.a;
// var b = obj.numbers.b;
// ES2015 Object Destructuring:
const obj = {
numbers: {
a: 1,
b: 2
}
};
const {numbers: {a, b}} = obj;
///////////////////////////ES5 Array Swap//////////////////////////////////
// var arr = [1, 2];
// var temp = arr[0];
// arr[0] = arr[1];
// arr[1] = temp;
///////////////ES2015 One-Line Array Swap with Destructuring///////////////
let arr = [1, 2];
[arr[0], arr[1]] = [arr[1], arr[0]];
///////////////////////////////////////////////////////////////## **raceResults()**////////////////////////////////////////////
// Write a function called ***raceResults*** which accepts a single array argument.
//It should return an object with the keys ***first***, ***second***, ***third***, and ***rest***.
// **Write a *one line* function to make this work using:
// (1) An arrow function, (2) Destructuring, (3 `Enhanced`) object assignment (same key/value shortcut).
const raceResults = ([first, second, third, ...rest]) => {return {first, second, third, rest}};