-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdestructuring.js
159 lines (96 loc) · 3.29 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
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
/**
Exercise 1
Rewrite the code below to use array destructuring instead of assigning each value to a variable.
Note: this test will pass even if array destructuring has not been used.
*/
let item = ["Egg", 0.25, 12];
// Change below this line
let name = item[0];
let price = item[1];
let quantity = item[2];
// Change above this line
// ---------------------------------------------------------------------
/*
Exercise 2
Rewrite the code below to assign each number to the right variable.
*/
let numbers = [3, 5, 4, 2, 6, 1];
// Change below this line
let [one, two, three, four, five, six] = numbers;
// Change above this line
// ---------------------------------------------------------------------
/**
Exercise 3a
We have an object called 'john'.
Write the destructuring assignment that reads:
- 'name' property into the variable 'johnName'.
- 'years' property into the variable 'johnAge'.
- 'isAdmin' property into the variable 'isJohnAdmin'
*/
let john = { name: "John", years: 30, isAdmin: true };
// Change below this line
let johnName = john.name
let johnAge = john.years
let isJohnAdmin = john.isAdmin
// Change above this line
// Exercise 3b
// We have an object called 'steve'. Similiar as before
// 'name' property into the variable 'steveName'.
// - 'years' property into the variable 'steveAge'.
// - 'isAdmin' property into the variable 'isSteveAdmin' (false, if no such property)
let steve = { name: "Steve", years: 25 };
// Change below this line
let steveName = steve.name
let steveAge = steve.years
let isSteveAdmin = steve.isAdmin
// Change above this line
// ---------------------------------------------------------------------
/**
Exercise 4a
Rewrite the code below to use array destructuring instead of assigning each value to a variable.
*/
let person = [12, "Chris", "Owen"];
// Change below this line
let firstName = person[1];
let lastName = person[2];
let age = person[0];
// Change above this line
/**
Exercise 4b
Rewrite the code below to use array destructuring instead of assigning each value to a variable.
Make sure not to have unused variables.
*/
let person2 = ["Chris", 12, "Owen"];
// Change below this line
let firstName2 = person2[0];
let lastName2 = person2[2];
// Change above this line
// ---------------------------------------------------------------------
/*
Exercise 5
Use Array Destructuring get the last name from the array.
*/
const students = ['Christina', 'Jon', 'Alexandare'];
// Change below this line
const lastStudent = ""
// Change above this line
// ---------------------------------------------------------------------
/**
Exercise 6
Using Array Destructuring get all of the names from this Nested Array
*/
const moreStudents = [
'Jeff',
['Abed', 'Troy'],
['Annie', 'Britta']
];
// Change below this line
let student1 = "" // Jeff
let student2 = "" // Abed
let student3 = "" // Troy
let student4 = "" // Annie
let student5 = "" // Britta
// Change above this line
// ---------------------------------------------------------------------
// DO NOT CHANGE CODE BELOW THIS LINE
module.exports = { name, quantity, price, one, two, three, four, five, six, johnName, johnAge, isJohnAdmin, steveName, steveAge, isSteveAdmin, firstName, lastName, age, firstName2, lastName2, lastStudent, student1, student2, student3, student4, student5 }