forked from WDI-SEA/object-array-drills
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercises.js
276 lines (197 loc) · 6.78 KB
/
exercises.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
////////////////////////////////////////////////
// Part 1: Working With Data Structures
////////////////////////////////////////////////
const album1 = {
title: "Talking Heads",
albumDetails: {
released: new Date("September 16, 1977"),
label: "Sire",
formats: ["LP"]
}
}
// 1. Retrieve the string "Sire" from album1, and save it in a sensibly named
// variable.
// ANSWER 1
const firstLabel = console.log(album1.albumDetails.label)
// 2. Change the title of album1 from "Talking Heads" to "Talking Heads: 77"
// ANSWER 2
album1.title = 'Talking Heads: 77'
console.log(album1.title)
const album2 = {
title: "More Songs About Buildings and Food",
albumDetails: {
released: new Date("July 14, 1978"),
label: "Sire",
formats: ["LP", "8-track"]
}
}
const album3 = {
title: "Fear of Music",
albumDetails: {
released: "August 3, 1979",
label: "Sire",
formats: ["Cassette"]
}
}
// 3. Access album2's formats array and use an array method to add "LP" to
// album3's formats
// Check out the Array.push method!
// ANSWER 3
// why does this give me 2?
// solved during prework, was using unnecessary formatting
album3.albumDetails.formats.push(album2.albumDetails.formats[0])
console.log(album3.albumDetails.formats)
// 4. Change the release date of album3 from a string into a Date object
// Look ahead to album4 for a clue!
// ANSWER4
//why does date turn into a purple number?
album3.albumDetails.released = new Date("August 3, 1979")
console.log(album3.albumDetails.released)
const album4 = {
title: "Remain in Light",
albumDetails: {
released: new Date("October 8, 1980"),
formats: ["Cassette", "LP"]
}
}
// 5. Add the label "Sire" to album4's details
//ANSWER 5
album4.albumDetails.label = "Sire"
console.log(album4.albumDetails)
const album5 = {
title: "Speaking in Tongues",
albumDetails: {
released: new Date("May 31, 1983"),
label: "Sire"
}
}
// 6. Add a 'formats' array to album 5 and add "CD", "Cassette", and "LP"
// ANSWER 7
album5.albumDetails.formats = ["CD", "Cassette", "LP"]
console.log(album5.albumDetails)
const album6 = {
title: "Little Creatures",
albumDetails: {
released: new Date("June 10, 1985"),
labels: ["Sire", "emi"],
formats: ["CD", "cassette", "LP"]
}
}
// 7. Make the label "emi" in album6 all uppercase
// google how to make a string uppercase in js!
// ANSWER 8
album6.albumDetails.labels[1] = album6.albumDetails.labels[1].toUpperCase()
console.log(album6.albumDetails.labels[1])
const album7 = {
title: "True Stories",
albumDetails: {
released: new Date("October 7, 1986"),
labels: "Sire, EMI",
formats: ["CD", "cassette", "LP"]
}
}
// 8. Convert album7's 'labels' property from the string value
// "Sire, EMI" into the array: ["Sire", "EMI"]
// google js array split!
// ANSWER 9
album7.albumDetails.labels = album7.albumDetails.labels.split(', ')
console.log(album7.albumDetails.labels)
/////////////////////////////////////////////////////
// Part 2: More Tasks About Datatypes and Structures
/////////////////////////////////////////////////////
const album8 = {
title: "Naked",
albumDetails: {
released: new Date("March 15, 1988"),
labels: ["Sire", "EMI"],
formats: ["CD", "cassette", "LP"]
}
}
const talkingHeadsAlbums = [
album1,
album2,
album3,
album4,
album5,
album6,
album7,
album8,
]
// 1. Create an object literal called `band`.
const band = {
}
// 2. Give it the property `name` and set it to "Talking Heads"
band.name = 'Talking Heads'
// 3. Give it the property `members` and set it to an array with a single
// string, "David Byrne", in it.
band.members = ['David Byrne']
// 4. Give it the property `albums` and set it to the array stored in the
// variable talkingHeadsAlbums
band.albums = talkingHeadsAlbums
// 5. Add "Tiny Weymouth", "Chris Franz" and "Jerry Harrison" to the members
// array.
band.members.push('Tiny Weymouth', 'Chris Franz', 'Jerry Harrison')
console.log(band)
////////////////////////////////////////////////
// Part 3: Conditional Logic
////////////////////////////////////////////////
// 1. Write a conditional to console.log "Talking Heads were a prolific band"
if(talkingHeadsAlbums.length >= 6){
console.log('The Talking Heads were a prolific band')
}else{
console.log('The Talking Heads didn\'t have much input')
}
// if the Talking Heads have 6 albums or more. Otherwise, console.log
// "Talking heads didn't have much output." Use the array of albums
//talkingHeadsAlbums above.
if(talkingHeadsAlbums.length % 2 == 0) {
console.log(talkingHeadsAlbums.length + ' is an even number')
}else{
console.log(talkingHeadsAlbums.length + 'is an odd number')
}
// 2. Write a conditional to check if the number of albums in
// talkingHeadsAlbums is odd or even, and then console.log
// "The number X is odd" or "The number X is even" with X being
// the number of albums.
// 3. Write conditionals to check if the number of albums in
// talkingHeadsAlbums is divisible by either 2 or 3, and then
// console.log one of:
// - "The number Y is divisible by 2",
// - "The number Y is divisible by 3",
// - "The number Y is divisible by 2 and 3", or
// - "The number Y is not divisible by 2 or 3",
//
// with Y being the number of albums.
if(talkingHeadsAlbums.length % 2 === 0 && talkingHeadsAlbums % 3 === 0){
console.log(talkingHeadsAlbums.length + ' is divisible by 2 AND 3')
}else if(talkingHeadsAlbums.length % 2 === 0) {
console.log(talkingHeadsAlbums.length + ' is divisible by 2')
}else if(talkingHeadsAlbums.length % 3 === 0){
console.log(talkingHeadsAlbums.length + ' is divisible by 3')
}else{
console.log(talkingHeadsAlbums.length + ' is not divisible by 2 AND 3')
}
// 4. Check your logic above against the numbers: 0, 1, 2, 6, 7, and 9.
// Make sure it always works!
/////////////////////////////////////////////////////
// Part 4: For Loops
/////////////////////////////////////////////////////
// 1. Use a for loop to print out the name of each Talking Heads album
// ANSWER 1
for(let i = 0; i < talkingHeadsAlbums.length; i++){
console.log(talkingHeadsAlbums[i].title)
}
// 2. Create a variable called `sireTally`, and set it to the integer value 0.
// Then use a for-loop to go through all the Talking Heads albums,
// incrementing sireTally if the album was released under the "Sire" label.
// Warning: some albums have a property `.label`, which is a string, and some
// have `.labels`, which is an Array!
// ANSWER 2
// || is equal to 'or' in Python
let sireTally = 0
for(let i = 0; i < talkingHeadsAlbums.length; i++){
if(talkingHeadsAlbums[i].albumDetails.label === "Sire" || talkingHeadsAlbums[i].albumDetails.labels.includes('Sire')){
sireTally++
}
}
console.log(sireTally)