-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhomework3_objects.js
More file actions
249 lines (221 loc) · 6.52 KB
/
Copy pathhomework3_objects.js
File metadata and controls
249 lines (221 loc) · 6.52 KB
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
/*
Homework 3 - JavaScript Fundamentals
COMP420 - Spring 2017
J. Wangsadinata
*/
/****************
**** OBJECTS ****
*****************/
var flight = {
airline: "Oceanic",
number: 815,
departure: {
IATA: "SYD",
time: "2004-09-22 22:36",
city: "Sydney"
},
arrival: {
IATA: "LAX",
time: "2004-09-23 10:42",
city: "Los Angeles"
}
};
var facebook = {
"data": [
{
"id": "X999_Y999",
"from": {
"name": "Tom Brady", "id": "X12"
},
"message": "Looking forward to 2017!",
"actions": [
{
"name": "Comment",
"link": "http://www.facebook.com/X999/posts/Y999"
},
{
"name": "Like",
"link": "http://www.facebook.com/X999/posts/Y999"
}
],
"type": "status",
"created_time": "2016-12-26T21:27:44+0000",
"updated_time": "2016-12-26T21:27:44+0000"
},
{
"id": "X998_Y998",
"from": {
"name": "Peyton Manning", "id": "X18"
},
"message": "Where's my contract?",
"actions": [
{
"name": "Comment",
"link": "http://www.facebook.com/X998/posts/Y998"
},
{
"name": "Like",
"link": "http://www.facebook.com/X998/posts/Y998"
}
],
"type": "status",
"created_time": "2016-08-02T21:27:44+0000",
"updated_time": "2016-08-02T21:27:44+0000"
}
]
};
var youtube = {
"apiVersion": "2.0",
"data": {
"updated": "2010-01-07T19:58:42.949Z",
"totalItems": 800,
"startIndex": 1,
"itemsPerPage": 1,
"items": [
{
"id": "hYB0mn5zh2c",
"uploaded": "2007-06-05T22:07:03.000Z",
"updated": "2010-01-07T13:26:50.000Z",
"uploader": "GoogleDeveloperDay",
"category": "News",
"title": "Google Developers Day US - Maps API Introduction",
"description": "Google Maps API Introduction ...",
"tags": [
"GDD07","GDD07US","Maps"
],
"thumbnail": {
"default":"http://i.ytimg.com/vi/hYB0mn5zh2c/default.jpg",
"hqDefault":"http://i.ytimg.com/vi/hYB0mn5zh2c/hqdefault.jpg"
},
"player": {
"default":"http://www.youtube.com/watch?vu003dhYB0mn5zh2c"
},
"content": {
"1":"rtsp://v5.cache3.c.youtube.com/CiILENy.../0/0/0/video.3gp",
"5":"http://www.youtube.com/v/hYB0mn5zh2c?f...",
"6":"rtsp://v1.cache1.c.youtube.com/CiILENy.../0/0/0/video.3gp"
},
"duration":2840,
"aspectRatio":"widescreen",
"rating":4.63,
"ratingCount":68,
"viewCount":220101,
"favoriteCount":201,
"commentCount":22,
"status": {
"value":"restricted",
"reason":"limitedSyndication"
},
"accessControl": {
"syndicate":"allowed",
"commentVote":"allowed",
"rate":"allowed",
"list":"allowed",
"comment":"allowed",
"embed":"allowed",
"videoRespond":"moderated"
}
}
]
}
};
/******************
**** QUESTIONS ****
*******************/
/* FLIGHTS */
/*
Task 1. Return the flight number
In the object above, your function should return 815.
(Use return statements instead of console.log(...))
*/
function flight_number(flight) {
return flight.number;
}
/*
Task 2. Return a string of the flight origin and destination separated by '-'
In the object above, your function should return 'Sydney-Los Angeles'
*/
function flight_destination(flight) {
return flight.departure.city + "-" + flight.arrival.city;
}
/*
BONUS. Return the duration of the flight (you can assume they are in the same time zone)
In the object above, your function should return '12 hours 6 minutes', or some variant of this.
*/
function flight_duration(flight) {
// Time difference is given in millisecond. To convert to minutes, divide by (60 * 1000).
var t = (new Date(flight.arrival.time) - new Date(flight.departure.time))/60000;
return Math.floor(t/60) + " hours " + (t % 60) + " minutes";
}
/* FACEBOOK */
/*
Task 1. Return a list of usernames on the object
In the object above, your function should return ['Tom Brady', 'Peyton Manning']
*/
function facebook_usernames(facebook) {
return facebook.data.map(function(a) { return a.from.name; });
}
/*
Task 2. Return an object indexed by the username, that has the status as its values.
In the object above, your function should return
[{ name: "Tom Brady", status: "Looking forward to 2017!"}, {name: "Peyton Manning", status: "Where's my contract?"}]
*/
function facebook_statuses(facebook) {
return facebook.data.map(function(a) { return {name: a.from.name, status: a.message}});
}
/*
Task 3. Create a function that takes in the output of your task 2, and
returns a string of the following form. "<username> says <status>".
Using the printing suite provided below, it will print out the following:
Tom Brady says Looking forward to 2017!
Peyton Manning says Where's my contract?
*/
function facebook_what_people_say(statuses) {
return statuses.map(function(a) { return a.name + " says " + a.status; }).join("\n");
}
/* YOUTUBE */
/*
Task 1. Return the title of the Youtube video.
In the object above, your function should return
'Google Developers Day US - Maps API Introduction'
*/
function youtube_video_title(youtube) {
return youtube.data.items[0].title;
}
/*
Task 2. Return the URL of the Youtube video.
In the object above, your function should return
'http://www.youtube.com/watch?vu003dhYB0mn5zh2c'
*/
function youtube_video_url(youtube) {
return youtube.data.items[0].player.default;
}
/*
Task 3. Given the object, return the duration of the Youtube video in minutes and seconds.
In the object above, your function should return
'47 minutes 20 seconds'
*/
function youtube_video_duration(youtube) {
var duration = youtube.data.items[0].duration;
return Math.floor(duration / 60) + " minutes " + (duration % 60) + " seconds";
}
/***********************
**** PRINTING SUITE ****
************************/
/* FLIGHTS */
console.log("### FLIGHTS ###");
console.log(flight_number(flight));
console.log(flight_destination(flight));
console.log(flight_duration(flight));
console.log("");
/* FACEBOOK */
console.log("### FACEBOOK ###");
console.log(facebook_usernames(facebook));
console.log(facebook_statuses(facebook));
console.log(facebook_what_people_say(facebook_statuses(facebook)));
console.log("");
/* YOUTUBE */
console.log("### YOUTUBE ###");
console.log(youtube_video_title(youtube));
console.log(youtube_video_url(youtube));
console.log(youtube_video_duration(youtube));