-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
1709 lines (1471 loc) · 64.1 KB
/
index.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Learn About Functional Programming
// The members of freeCodeCamp happen to love tea.
// In the code editor, the "prepareTea" and "getTea" functions are already defined for you.
// Call the "getTea" function to get 40 cups of tea for the team,
// and store them in the "tea4TeamFCC" variable.
// Function that returns a string representing a cup of green tea
const prepareTea = () => 'greenTea';
/*
Given a function (representing the tea type) and number of cups needed,
the following function returns an array of strings
(each representing a cup of a specific type of tea).
*/
const getTea = (numOfCups) => {
const teaCups = [];
for(let cups = 1; cups <= numOfCups; cups += 1) {
const teaCup = prepareTea();
teaCups.push(teaCup);
}
return teaCups;
};
const tea4TeamFCC = getTea(40);
console.log(tea4TeamFCC);
// Understand Functional Programming Terminology
// Prepare 27 cups of green tea and 13 cups of black tea
// and store them in tea4GreenTeamFCC and tea4BlackTeamFCC variables, respectively.
// Note that the getTea function has been modified so it now takes a function
// as the first argument.
// Note: The data (the number of cups of tea) is supplied as the last argument.
// We'll discuss this more in later lessons.
// Function that returns a string representing a cup of green tea
const prepareGreenTea = () => 'greenTea';
// Function that returns a string representing a cup of black tea
const prepareBlackTea = () => 'blackTea';
/*
Given a function (representing the tea type) and number of cups needed, the
following function returns an array of strings (each representing a cup of
a specific type of tea).
*/
const getTea = (prepareTea, numOfCups) => {
const teaCups = [];
for(let cups = 1; cups <= numOfCups; cups += 1) {
const teaCup = prepareTea();
teaCups.push(teaCup);
}
return teaCups;
};
const tea4GreenTeamFCC = getTea(prepareGreenTea, 27);
const tea4BlackTeamFCC = getTea(prepareBlackTea, 13);
console.log(
tea4GreenTeamFCC,
tea4BlackTeamFCC);
// Understand the Hazards of Using Imperative Code
// Examine the code in the editor. It's using a method that has side effects in the program,
// causing incorrect behaviour. The final list of open tabs, stored in "finalTabs.tabs",
// should be "['FB', 'Gitter', 'Reddit', 'Twitter', 'Medium', 'new tab', 'Netflix', 'YouTube',
// 'Vine', 'GMail', 'Work mail', 'Docs', 'freeCodeCamp', 'new tab']" but
// the list produced by the code is slightly different.
// Change "Window.prototype.tabClose" so that it removes the correct tab.
// tabs is an array of titles of each site open within the window
const Window = function(tabs) {
this.tabs = tabs; // We keep a record of the array inside the object
};
// When you join two windows into one window
Window.prototype.join = function(otherWindow) {
this.tabs = this.tabs.concat(otherWindow.tabs);
return this;
};
// When you open a new tab at the end
Window.prototype.tabOpen = function(tab) {
this.tabs.push('new tab'); // Let's open a new tab for now
return this;
};
// When you close a tab
Window.prototype.tabClose = function(index) {
// Only change code below this line
const tabsBeforeIndex = this.tabs.splice(0, index); // Get the tabs before the tab
const tabsAfterIndex = this.tabs.splice(1); // Get the tabs after the tab
this.tabs = tabsBeforeIndex.concat(tabsAfterIndex); // Join them together
// Only change code above this line
return this;
};
// Let's create three browser windows
const workWindow = new Window(['GMail', 'Inbox', 'Work mail', 'Docs', 'freeCodeCamp']);
// Your mailbox, drive, and other work sites
const socialWindow = new Window(['FB', 'Gitter', 'Reddit', 'Twitter', 'Medium']);
// Social sites
const videoWindow = new Window(['Netflix', 'YouTube', 'Vimeo', 'Vine']);
// Entertainment sites
// Now perform the tab opening, closing, and other operations
const finalTabs = socialWindow
.tabOpen() // Open a new tab for cat memes
.join(videoWindow.tabClose(2)) // Close third tab in video window, and join
.join(workWindow.tabClose(1).tabOpen());
console.log(finalTabs.tabs);
// Avoid Mutations and Side Effects Using Functional Programming
// Fill in the code for the function incrementer so it returns the value
// of the global variable fixedValue increased by one.
// The global variable
let fixedValue = 4;
function incrementer() {
// Only change code below this line
let newValue = fixedValue + 1;
return newValue;
// Only change code above this line
}
console.log(incrementer()); // 5
// Pass Arguments to Avoid External Dependence in a Function
// Let's update the "incrementer" function to clearly declare its dependencies.
// Write the "incrementer" function so it takes an argument,
// and then returns a result after increasing the value by one.
// The global variable
let fixedValue = 4;
// Only change code below this line
function incrementer(fixedValue) {
return fixedValue + 1;
// Only change code above this line
}
// Refactor Global Variables Out of Functions
// * Reminder *
// https://codeburst.io/javascript-passing-by-value-vs-reference-explained-in-plain-english-8d00fd06a47c
// changing a primitive value
let a = 1;
let b = a;
console.log(a); // 1
console.log(b); // 1
a = 2;
console.log(a); // 2
console.log(b); // 1
// changing a object
const arrVar = { a: 0, b: 1, c: 2 };
const newArr = arrVar;
newArr.c = 3;
console.log(newArr); // { a: 0, b: 1, c: 3 }
console.log(arrVar); // { a: 0, b: 1, c: 3 }
arrVar.c = 2;
console.log(newArr); // { a: 0, b: 1, c: 2 }
console.log(arrVar); // { a: 0, b: 1, c: 3 }
// changing a value in "newArr" would change the value in "arrVar"
const arrVar = [0, 1, 2];
const newArr = arrVar;
newArr.push(3);
console.log(newArr); // [ 0, 1, 2, 3 ]
console.log(arrVar); // [ 0, 1, 2, 3 ]
arrVar.push(4);
console.log(newArr); // [ 0, 1, 2, 3, 4 ]
console.log(arrVar); // [ 0, 1, 2, 3, 4 ]
const arrVar = [0, 1, 2];
function newArray(array) {
let newArr = [];
return newArr.concat(arrVar, array);
}
console.log(newArray([3])); // [ 0, 1, 2, 3 ]
console.log(arrVar); // [ 0, 1, 2 ]
// Rewrite the code so the global array bookList is not changed inside either function.
// The add function should add the given bookName to the end of the array passed to it
// and return a new array (list). The remove function should remove the given bookName
// from the array passed to it.
// Note: Both functions should return an array, and any new parameters should be added
// before the bookName parameter.
// The global variable
const bookList = [
"The Hound of the Baskervilles",
"On The Electrodynamics of Moving Bodies",
"Philosophiæ Naturalis Principia Mathematica",
"Disquisitiones Arithmeticae"
];
// Change code below this line
// function add (bookName) {
// bookList.push(bookName);
// return bookList;
// // Change code above this line
// }
/* This function should add a book to the list and return the list */
// New parameters should come before bookName
function add (arr, bookName) {
let newArr = [...arr]; // Copy the bookList array to a new array.
newArr.push(bookName); // Add bookName parameter to the end of the new array.
return newArr; // Return the new array.
}
// Change code below this line
// function remove (bookName) {
// let newBookName = []
// newBookName.push[bookName];
// const book_index = bookList.indexOf(newBookName);
// if (book_index >= 0) {
// bookList.splice(book_index, 1);
// return bookList;
// // Change code above this line
// }
// }
/* This function should remove a book from the list and return the list */
// New parameters should come before the bookName one
function remove(arr, bookName) {
let newArr = [...arr]; // Copy the bookList array to a new array.
if (newArr.indexOf(bookName) >= 0) {
// Check whether the bookName parameter is in new array.
newArr.splice(newArr.indexOf(bookName), 1); // Remove the given paramater
// from the new array.
return newArr; // Return the new array.
}
}
const newBookList = add (bookList, 'A Brief History of Time');
const newerBookList = remove (bookList, 'On The Electrodynamics of Moving Bodies');
const newestBookList = remove (add(
bookList, 'A Brief History of Time'
),'On The Electrodynamics of Moving Bodies'
);
console.log(bookList);
// Use the map Method to Extract Data from an Array
// The "watchList" array holds objects with information on several movies.
// Use "map" on "watchList" to assign a new array of objects to the "ratings" variable.
// Each movie in the new array should have only a "title" key with the name of the film,
// and a "rating" key with the IMDB rating. The code in the editor currently uses
// a "for" loop to do this, so you should replace the loop functionality
// with your "map" expression.
// ratings should equal
// [
// {"title": "Inception", "rating": "8.8"},
// {"title": "Interstellar", "rating": "8.6"},
// {"title": "The Dark Knight", "rating": "9.0"},
// {"title": "Batman Begins", "rating": "8.3"},
// {"title": "Avatar", "rating": "7.9"}
// ]
// The global variable
const watchList = [
{
"Title": "Inception",
"Year": "2010",
"Rated": "PG-13",
"Released": "16 Jul 2010",
"Runtime": "148 min",
"Genre": "Action, Adventure, Crime",
"Director": "Christopher Nolan",
"Writer": "Christopher Nolan",
"Actors": "Leonardo DiCaprio, Joseph Gordon-Levitt, Elliot Page, Tom Hardy",
"Plot": "A thief, who steals corporate secrets through use of dream-sharing technology, is given the inverse task of planting an idea into the mind of a CEO.",
"Language": "English, Japanese, French",
"Country": "USA, UK",
"Awards": "Won 4 Oscars. Another 143 wins & 198 nominations.",
"Poster": "http://ia.media-imdb.com/images/M/MV5BMjAxMzY3NjcxNF5BMl5BanBnXkFtZTcwNTI5OTM0Mw@@._V1_SX300.jpg",
"Metascore": "74",
"imdbRating": "8.8",
"imdbVotes": "1,446,708",
"imdbID": "tt1375666",
"Type": "movie",
"Response": "True"
},
{
"Title": "Interstellar",
"Year": "2014",
"Rated": "PG-13",
"Released": "07 Nov 2014",
"Runtime": "169 min",
"Genre": "Adventure, Drama, Sci-Fi",
"Director": "Christopher Nolan",
"Writer": "Jonathan Nolan, Christopher Nolan",
"Actors": "Ellen Burstyn, Matthew McConaughey, Mackenzie Foy, John Lithgow",
"Plot": "A team of explorers travel through a wormhole in space in an attempt to ensure humanity's survival.",
"Language": "English",
"Country": "USA, UK",
"Awards": "Won 1 Oscar. Another 39 wins & 132 nominations.",
"Poster": "http://ia.media-imdb.com/images/M/MV5BMjIxNTU4MzY4MF5BMl5BanBnXkFtZTgwMzM4ODI3MjE@._V1_SX300.jpg",
"Metascore": "74",
"imdbRating": "8.6",
"imdbVotes": "910,366",
"imdbID": "tt0816692",
"Type": "movie",
"Response": "True"
},
{
"Title": "The Dark Knight",
"Year": "2008",
"Rated": "PG-13",
"Released": "18 Jul 2008",
"Runtime": "152 min",
"Genre": "Action, Adventure, Crime",
"Director": "Christopher Nolan",
"Writer": "Jonathan Nolan (screenplay), Christopher Nolan (screenplay), Christopher Nolan (story), David S. Goyer (story), Bob Kane (characters)",
"Actors": "Christian Bale, Heath Ledger, Aaron Eckhart, Michael Caine",
"Plot": "When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, the caped crusader must come to terms with one of the greatest psychological tests of his ability to fight injustice.",
"Language": "English, Mandarin",
"Country": "USA, UK",
"Awards": "Won 2 Oscars. Another 146 wins & 142 nominations.",
"Poster": "http://ia.media-imdb.com/images/M/MV5BMTMxNTMwODM0NF5BMl5BanBnXkFtZTcwODAyMTk2Mw@@._V1_SX300.jpg",
"Metascore": "82",
"imdbRating": "9.0",
"imdbVotes": "1,652,832",
"imdbID": "tt0468569",
"Type": "movie",
"Response": "True"
},
{
"Title": "Batman Begins",
"Year": "2005",
"Rated": "PG-13",
"Released": "15 Jun 2005",
"Runtime": "140 min",
"Genre": "Action, Adventure",
"Director": "Christopher Nolan",
"Writer": "Bob Kane (characters), David S. Goyer (story), Christopher Nolan (screenplay), David S. Goyer (screenplay)",
"Actors": "Christian Bale, Michael Caine, Liam Neeson, Katie Holmes",
"Plot": "After training with his mentor, Batman begins his fight to free crime-ridden Gotham City from the corruption that Scarecrow and the League of Shadows have cast upon it.",
"Language": "English, Urdu, Mandarin",
"Country": "USA, UK",
"Awards": "Nominated for 1 Oscar. Another 15 wins & 66 nominations.",
"Poster": "http://ia.media-imdb.com/images/M/MV5BNTM3OTc0MzM2OV5BMl5BanBnXkFtZTYwNzUwMTI3._V1_SX300.jpg",
"Metascore": "70",
"imdbRating": "8.3",
"imdbVotes": "972,584",
"imdbID": "tt0372784",
"Type": "movie",
"Response": "True"
},
{
"Title": "Avatar",
"Year": "2009",
"Rated": "PG-13",
"Released": "18 Dec 2009",
"Runtime": "162 min",
"Genre": "Action, Adventure, Fantasy",
"Director": "James Cameron",
"Writer": "James Cameron",
"Actors": "Sam Worthington, Zoe Saldana, Sigourney Weaver, Stephen Lang",
"Plot": "A paraplegic marine dispatched to the moon Pandora on a unique mission becomes torn between following his orders and protecting the world he feels is his home.",
"Language": "English, Spanish",
"Country": "USA, UK",
"Awards": "Won 3 Oscars. Another 80 wins & 121 nominations.",
"Poster": "http://ia.media-imdb.com/images/M/MV5BMTYwOTEwNjAzMl5BMl5BanBnXkFtZTcwODc5MTUwMw@@._V1_SX300.jpg",
"Metascore": "83",
"imdbRating": "7.9",
"imdbVotes": "876,575",
"imdbID": "tt0499549",
"Type": "movie",
"Response": "True"
}
];
// Only change code below this line
// using "for" loop
// const ratings = [];
// for (let i = 0; i < watchList.length; i++) {
// ratings.push({title: watchList[i]["Title"], rating: watchList[i]["imdbRating"]});
// }
// using "map" expression
const ratings = watchList.map(item => ({
title: item["Title"],
rating: item["imdbRating"]
}));
// or
// const ratings = watchList.map(({ Title: title, imdbRating: rating }) => ({title, rating}));
// Only change code above this line
console.log(JSON.stringify(ratings));
// [
// {"title":"Inception","rating":"8.8"},
// {"title":"Interstellar","rating":"8.6"},
// {"title":"The Dark Knight","rating":"9.0"},
// {"title":"Batman Begins","rating":"8.3"},
// {"title":"Avatar","rating":"7.9"}
// ]
// Implement map on a Prototype
// Write your own "Array.prototype.myMap()", which should behave exactly like "Array.prototype.map()".
// You should not use the built-in "map" method. The "Array" instance can be accessed
// in the "myMap" method using "this".
// The global variable
const s = [23, 65, 98, 5];
Array.prototype.myMap = function(callback) {
const newArray = [];
// Only change code below this line
for (let i = 0; i < this.length; i++) { // In the global execution context (outside of any function),
// "this" refers to the global object whether
// in strict mode or not
newArray.push(callback(this[i]));
}
// Only change code above this line
return newArray;
};
const new_s = s.myMap(function(item) {
return item * 2;
});
console.log(new_s); // [ 46, 130, 196, 10 ]
// Use the filter Method to Extract Data from an Array
const users = [
{ name: 'John', age: 34 },
{ name: 'Amy', age: 20 },
{ name: 'camperCat', age: 10 }
];
const usersUnder30 = users.filter(user => user.age < 30);
console.log(usersUnder30); // [ { name: 'Amy', age: 20 }, { name: 'camperCat', age: 10 } ]
const usersAmy = users.filter(user => user.name === 'Amy');
console.log(usersAmy); // [ { name: 'Amy', age: 20 } ]
// The variable "watchList" holds an array of objects with information on several movies.
// Use a combination of "filter" and "map" on "watchList" to assign a new array of objects
// with only "title" and "rating" keys. The new array should only include objects where
// "imdbRating" is greater than or equal to 8.0. Note that the "rating" values are saved
// as strings in the object and you may need to convert them into numbers to perform
// mathematical operations on them.
// The global variable
const watchList = [
{
"Title": "Inception",
"Year": "2010",
"Rated": "PG-13",
"Released": "16 Jul 2010",
"Runtime": "148 min",
"Genre": "Action, Adventure, Crime",
"Director": "Christopher Nolan",
"Writer": "Christopher Nolan",
"Actors": "Leonardo DiCaprio, Joseph Gordon-Levitt, Elliot Page, Tom Hardy",
"Plot": "A thief, who steals corporate secrets through use of dream-sharing technology, is given the inverse task of planting an idea into the mind of a CEO.",
"Language": "English, Japanese, French",
"Country": "USA, UK",
"Awards": "Won 4 Oscars. Another 143 wins & 198 nominations.",
"Poster": "http://ia.media-imdb.com/images/M/MV5BMjAxMzY3NjcxNF5BMl5BanBnXkFtZTcwNTI5OTM0Mw@@._V1_SX300.jpg",
"Metascore": "74",
"imdbRating": "8.8",
"imdbVotes": "1,446,708",
"imdbID": "tt1375666",
"Type": "movie",
"Response": "True"
},
{
"Title": "Interstellar",
"Year": "2014",
"Rated": "PG-13",
"Released": "07 Nov 2014",
"Runtime": "169 min",
"Genre": "Adventure, Drama, Sci-Fi",
"Director": "Christopher Nolan",
"Writer": "Jonathan Nolan, Christopher Nolan",
"Actors": "Ellen Burstyn, Matthew McConaughey, Mackenzie Foy, John Lithgow",
"Plot": "A team of explorers travel through a wormhole in space in an attempt to ensure humanity's survival.",
"Language": "English",
"Country": "USA, UK",
"Awards": "Won 1 Oscar. Another 39 wins & 132 nominations.",
"Poster": "http://ia.media-imdb.com/images/M/MV5BMjIxNTU4MzY4MF5BMl5BanBnXkFtZTgwMzM4ODI3MjE@._V1_SX300.jpg",
"Metascore": "74",
"imdbRating": "8.6",
"imdbVotes": "910,366",
"imdbID": "tt0816692",
"Type": "movie",
"Response": "True"
},
{
"Title": "The Dark Knight",
"Year": "2008",
"Rated": "PG-13",
"Released": "18 Jul 2008",
"Runtime": "152 min",
"Genre": "Action, Adventure, Crime",
"Director": "Christopher Nolan",
"Writer": "Jonathan Nolan (screenplay), Christopher Nolan (screenplay), Christopher Nolan (story), David S. Goyer (story), Bob Kane (characters)",
"Actors": "Christian Bale, Heath Ledger, Aaron Eckhart, Michael Caine",
"Plot": "When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, the caped crusader must come to terms with one of the greatest psychological tests of his ability to fight injustice.",
"Language": "English, Mandarin",
"Country": "USA, UK",
"Awards": "Won 2 Oscars. Another 146 wins & 142 nominations.",
"Poster": "http://ia.media-imdb.com/images/M/MV5BMTMxNTMwODM0NF5BMl5BanBnXkFtZTcwODAyMTk2Mw@@._V1_SX300.jpg",
"Metascore": "82",
"imdbRating": "9.0",
"imdbVotes": "1,652,832",
"imdbID": "tt0468569",
"Type": "movie",
"Response": "True"
},
{
"Title": "Batman Begins",
"Year": "2005",
"Rated": "PG-13",
"Released": "15 Jun 2005",
"Runtime": "140 min",
"Genre": "Action, Adventure",
"Director": "Christopher Nolan",
"Writer": "Bob Kane (characters), David S. Goyer (story), Christopher Nolan (screenplay), David S. Goyer (screenplay)",
"Actors": "Christian Bale, Michael Caine, Liam Neeson, Katie Holmes",
"Plot": "After training with his mentor, Batman begins his fight to free crime-ridden Gotham City from the corruption that Scarecrow and the League of Shadows have cast upon it.",
"Language": "English, Urdu, Mandarin",
"Country": "USA, UK",
"Awards": "Nominated for 1 Oscar. Another 15 wins & 66 nominations.",
"Poster": "http://ia.media-imdb.com/images/M/MV5BNTM3OTc0MzM2OV5BMl5BanBnXkFtZTYwNzUwMTI3._V1_SX300.jpg",
"Metascore": "70",
"imdbRating": "8.3",
"imdbVotes": "972,584",
"imdbID": "tt0372784",
"Type": "movie",
"Response": "True"
},
{
"Title": "Avatar",
"Year": "2009",
"Rated": "PG-13",
"Released": "18 Dec 2009",
"Runtime": "162 min",
"Genre": "Action, Adventure, Fantasy",
"Director": "James Cameron",
"Writer": "James Cameron",
"Actors": "Sam Worthington, Zoe Saldana, Sigourney Weaver, Stephen Lang",
"Plot": "A paraplegic marine dispatched to the moon Pandora on a unique mission becomes torn between following his orders and protecting the world he feels is his home.",
"Language": "English, Spanish",
"Country": "USA, UK",
"Awards": "Won 3 Oscars. Another 80 wins & 121 nominations.",
"Poster": "http://ia.media-imdb.com/images/M/MV5BMTYwOTEwNjAzMl5BMl5BanBnXkFtZTcwODc5MTUwMw@@._V1_SX300.jpg",
"Metascore": "83",
"imdbRating": "7.9",
"imdbVotes": "876,575",
"imdbID": "tt0499549",
"Type": "movie",
"Response": "True"
}
];
// Only change code below this line
const filteredList = watchList
.map(movie => {
return {
title: movie.Title, // or // title: movie["Title"],
rating: movie.imdbRating // or // title: movie["imdbRating"],
};
})
.filter(movie => {
// return true it will keep the item
// return false it will reject the item
return parseFloat(movie.rating) >= 8.0;
});
// Only change code above this line
console.log(filteredList);
// [
// { title: 'Inception', rating: '8.8' },
// { title: 'Interstellar', rating: '8.6' },
// { title: 'The Dark Knight', rating: '9.0' },
// { title: 'Batman Begins', rating: '8.3' }
// ]
// Return Part of an Array Using the "slice" Method
const arr = ["Cat", "Dog", "Tiger", "Zebra"];
const newArray = arr.slice(1, 3);
console.log(newArray); // [ 'Dog', 'Tiger' ]
console.log(arr); // [ 'Cat', 'Dog', 'Tiger', 'Zebra' ]
// Use the "slice" method in the "sliceArray" function to return part
// of the "anim" array given the provided "beginSlice" and "endSlice" indices.
// The function should return an array.
function sliceArray(anim, beginSlice, endSlice) {
return anim.slice(beginSlice, endSlice);
}
const inputAnim = ["Cat", "Dog", "Tiger", "Zebra", "Ant"];
console.log(sliceArray(inputAnim, 1, 3)); // [ 'Dog', 'Tiger' ]
console.log(sliceArray(inputAnim, 0, 1)); // [ 'Cat' ]
console.log(sliceArray(inputAnim, 1, 4)); // [ 'Dog', 'Tiger', 'Zebra' ]
// Remove Elements from an Array Using ``slice`` Instead of ``splice``
const cities = ["Chicago", "Delhi", "Islamabad", "London", "Berlin"];
console.log(cities.splice(3, 1)); // [ 'London' ]
console.log(cities); // [ 'Chicago', 'Delhi', 'Islamabad', 'Berlin' ]
// Rewrite the function nonMutatingSplice by using slice instead of splice.
// It should limit the provided cities array to a length of 3,
// and return a new array with only the first three items.
// Do not mutate the original array provided to the function.
// nonMutatingSplice(["Chicago", "Delhi", "Islamabad", "London", "Berlin"])
// should return ["Chicago", "Delhi", "Islamabad"].
function nonMutatingSplice(cities) {
// Only change code below this line
return cities.splice(3);
// Only change code above this line
}
const inputCities = ["Chicago", "Delhi", "Islamabad", "London", "Berlin"];
console.log(nonMutatingSplice(inputCities)); // [ 'London', 'Berlin' ]
console.log(inputCities); // [ 'Chicago', 'Delhi', 'Islamabad' ]
// solution:
function nonMutatingSplice(cities) {
return cities.slice(0, 3);
}
const inputCities = ["Chicago", "Delhi", "Islamabad", "London", "Berlin"];
console.log(nonMutatingSplice(inputCities));
// [ 'Chicago', 'Delhi', 'Islamabad' ]
// Combine Two Arrays Using the "concat" Method
let concatenation = [1, 2, 3].concat([4, 5, 6]);
console.log(concatenation); // [ 1, 2, 3, 4, 5, 6 ]
let array1 = [1, 2, 3];
let array2 = [4, 5, 6];
let concatenation = array1.concat(array2);
console.log(concatenation); // [ 1, 2, 3, 4, 5, 6 ]
console.log(array1); // [ 1, 2, 3 ]
console.log(array2); // [ 4, 5, 6 ]
// Use the "concat" method in the "nonMutatingConcat" function to concatenate "attach" to
// the end of "original". The function should return the concatenated array.
function nonMutatingConcat(original, attach) {
return original.concat(attach);
}
const first = [1, 2, 3];
const second = [4, 5];
console.log(nonMutatingConcat(first, second)); // [ 1, 2, 3, 4, 5 ]
console.log(first); // [ 1, 2, 3 ]
console.log(second); // [ 4, 5 ]
// Add Elements to the End of an Array Using concat Instead of push
const arr = [1, 2, 3];
arr.push([4, 5, 6]);
console.log(arr); // [ 1, 2, 3, [ 4, 5, 6 ] ]
// Change the nonMutatingPush function so it uses concat to add newItem
// to the end of original instead of push. The function should return an array.
function nonMutatingPush(original, newItem) {
// Only change code below this line
return original.push(newItem);
// Only change code above this line
}
const first = [1, 2, 3];
const second = [4, 5];
console.log(first); // [ 1, 2, 3 ]
console.log(second); // [ 4, 5 ]
nonMutatingPush(first, second);
console.log(first); // [ 1, 2, 3, [ 4, 5 ] ] // Mutated!!!
console.log(second); // [ 4, 5 ]
// Solution:
function nonMutatingPush(original, newItem) {
return original.concat(newItem);
}
const first = [1, 2, 3];
const second = [4, 5];
console.log(first); // [ 1, 2, 3 ]
console.log(second); // [ 4, 5 ]
console.log(nonMutatingPush(first, second)); // [ 1, 2, 3, 4, 5 ]
console.log(first); // [ 1, 2, 3 ]
console.log(second); // [ 4, 5 ]
// Use the ``reduce`` Method to Analyze Data
// See below for an example using "reduce" on the "users" array to return
// the sum of all the users' ages. For simplicity, the example
// only uses the first and second arguments.
const users = [
{ name: 'John', age: 34 },
{ name: 'Amy', age: 20 },
{ name: 'camperCat', age: 10 }
];
const sumOfAges = users.reduce((sum, user) => sum + user.age, 0);
console.log(sumOfAges); // 64
const users = [
{ name: 'John', age: 34 },
{ name: 'Amy', age: 20 },
{ name: 'camperCat', age: 10 }
];
const sumOfAges = users.reduce((sum, user) => sum + user.age, 2);
console.log(sumOfAges); // 66
// In another example, see how an object can be returned containing
// the names of the users as properties with their ages as values.
const users = [
{ name: 'John', age: 34 },
{ name: 'Amy', age: 20 },
{ name: 'camperCat', age: 10 }
];
const usersObj = users.reduce((obj, user) => {
obj[user.name] = user.age;
return obj;
}, {});
console.log(usersObj);
// The variable "watchList" holds an array of objects with information on several movies.
// Use "reduce" to find the average IMDB rating of the movies directed by "Christopher Nolan".
// Recall from prior challenges how to "filter" data and "map" over it to pull what you need.
// You may need to create other variables, and return the average rating from "getRating" function.
// Note that the rating values are saved as strings in the object
// and need to be converted into numbers before they are used in any mathematical operations.
// The global variable
const watchList = [
{
"Title": "Inception",
"Year": "2010",
"Rated": "PG-13",
"Released": "16 Jul 2010",
"Runtime": "148 min",
"Genre": "Action, Adventure, Crime",
"Director": "Christopher Nolan",
"Writer": "Christopher Nolan",
"Actors": "Leonardo DiCaprio, Joseph Gordon-Levitt, Elliot Page, Tom Hardy",
"Plot": "A thief, who steals corporate secrets through use of dream-sharing technology, is given the inverse task of planting an idea into the mind of a CEO.",
"Language": "English, Japanese, French",
"Country": "USA, UK",
"Awards": "Won 4 Oscars. Another 143 wins & 198 nominations.",
"Poster": "http://ia.media-imdb.com/images/M/MV5BMjAxMzY3NjcxNF5BMl5BanBnXkFtZTcwNTI5OTM0Mw@@._V1_SX300.jpg",
"Metascore": "74",
"imdbRating": "8.8",
"imdbVotes": "1,446,708",
"imdbID": "tt1375666",
"Type": "movie",
"Response": "True"
},
{
"Title": "Interstellar",
"Year": "2014",
"Rated": "PG-13",
"Released": "07 Nov 2014",
"Runtime": "169 min",
"Genre": "Adventure, Drama, Sci-Fi",
"Director": "Christopher Nolan",
"Writer": "Jonathan Nolan, Christopher Nolan",
"Actors": "Ellen Burstyn, Matthew McConaughey, Mackenzie Foy, John Lithgow",
"Plot": "A team of explorers travel through a wormhole in space in an attempt to ensure humanity's survival.",
"Language": "English",
"Country": "USA, UK",
"Awards": "Won 1 Oscar. Another 39 wins & 132 nominations.",
"Poster": "http://ia.media-imdb.com/images/M/MV5BMjIxNTU4MzY4MF5BMl5BanBnXkFtZTgwMzM4ODI3MjE@._V1_SX300.jpg",
"Metascore": "74",
"imdbRating": "8.6",
"imdbVotes": "910,366",
"imdbID": "tt0816692",
"Type": "movie",
"Response": "True"
},
{
"Title": "The Dark Knight",
"Year": "2008",
"Rated": "PG-13",
"Released": "18 Jul 2008",
"Runtime": "152 min",
"Genre": "Action, Adventure, Crime",
"Director": "Christopher Nolan",
"Writer": "Jonathan Nolan (screenplay), Christopher Nolan (screenplay), Christopher Nolan (story), David S. Goyer (story), Bob Kane (characters)",
"Actors": "Christian Bale, Heath Ledger, Aaron Eckhart, Michael Caine",
"Plot": "When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, the caped crusader must come to terms with one of the greatest psychological tests of his ability to fight injustice.",
"Language": "English, Mandarin",
"Country": "USA, UK",
"Awards": "Won 2 Oscars. Another 146 wins & 142 nominations.",
"Poster": "http://ia.media-imdb.com/images/M/MV5BMTMxNTMwODM0NF5BMl5BanBnXkFtZTcwODAyMTk2Mw@@._V1_SX300.jpg",
"Metascore": "82",
"imdbRating": "9.0",
"imdbVotes": "1,652,832",
"imdbID": "tt0468569",
"Type": "movie",
"Response": "True"
},
{
"Title": "Batman Begins",
"Year": "2005",
"Rated": "PG-13",
"Released": "15 Jun 2005",
"Runtime": "140 min",
"Genre": "Action, Adventure",
"Director": "Christopher Nolan",
"Writer": "Bob Kane (characters), David S. Goyer (story), Christopher Nolan (screenplay), David S. Goyer (screenplay)",
"Actors": "Christian Bale, Michael Caine, Liam Neeson, Katie Holmes",
"Plot": "After training with his mentor, Batman begins his fight to free crime-ridden Gotham City from the corruption that Scarecrow and the League of Shadows have cast upon it.",
"Language": "English, Urdu, Mandarin",
"Country": "USA, UK",
"Awards": "Nominated for 1 Oscar. Another 15 wins & 66 nominations.",
"Poster": "http://ia.media-imdb.com/images/M/MV5BNTM3OTc0MzM2OV5BMl5BanBnXkFtZTYwNzUwMTI3._V1_SX300.jpg",
"Metascore": "70",
"imdbRating": "8.3",
"imdbVotes": "972,584",
"imdbID": "tt0372784",
"Type": "movie",
"Response": "True"
},
{
"Title": "Avatar",
"Year": "2009",
"Rated": "PG-13",
"Released": "18 Dec 2009",
"Runtime": "162 min",
"Genre": "Action, Adventure, Fantasy",
"Director": "James Cameron",
"Writer": "James Cameron",
"Actors": "Sam Worthington, Zoe Saldana, Sigourney Weaver, Stephen Lang",
"Plot": "A paraplegic marine dispatched to the moon Pandora on a unique mission becomes torn between following his orders and protecting the world he feels is his home.",
"Language": "English, Spanish",
"Country": "USA, UK",
"Awards": "Won 3 Oscars. Another 80 wins & 121 nominations.",
"Poster": "http://ia.media-imdb.com/images/M/MV5BMTYwOTEwNjAzMl5BMl5BanBnXkFtZTcwODc5MTUwMw@@._V1_SX300.jpg",
"Metascore": "83",
"imdbRating": "7.9",
"imdbVotes": "876,575",
"imdbID": "tt0499549",
"Type": "movie",
"Response": "True"
}
];
function getRating(watchList) {
let christopherNolanMovies = watchList
.filter(item => item.Director === "Christopher Nolan")
.map(item => Number(item.imdbRating))
let averageRating = christopherNolanMovies
.reduce((previousValue, currentValue) => previousValue + currentValue) / christopherNolanMovies.length;
return averageRating;
}
console.log(getRating(watchList)); // 8.675
// or
// the global variable
var watchList = [
{
Title: "Inception",
Year: "2010",
Rated: "PG-13",
Released: "16 Jul 2010",
Runtime: "148 min",
Genre: "Action, Adventure, Crime",
Director: "Christopher Nolan",
Writer: "Christopher Nolan",
Actors: "Leonardo DiCaprio, Joseph Gordon-Levitt, Ellen Page, Tom Hardy",
Plot:
"A thief, who steals corporate secrets through use of dream-sharing technology, is given the inverse task of planting an idea into the mind of a CEO.",
Language: "English, Japanese, French",
Country: "USA, UK",
Awards: "Won 4 Oscars. Another 143 wins & 198 nominations.",
Poster:
"http://ia.media-imdb.com/images/M/MV5BMjAxMzY3NjcxNF5BMl5BanBnXkFtZTcwNTI5OTM0Mw@@._V1_SX300.jpg",
Metascore: "74",
imdbRating: "8.8",
imdbVotes: "1,446,708",
imdbID: "tt1375666",
Type: "movie",
Response: "True"
},
{
Title: "Interstellar",
Year: "2014",
Rated: "PG-13",
Released: "07 Nov 2014",
Runtime: "169 min",
Genre: "Adventure, Drama, Sci-Fi",
Director: "Christopher Nolan",
Writer: "Jonathan Nolan, Christopher Nolan",
Actors: "Ellen Burstyn, Matthew McConaughey, Mackenzie Foy, John Lithgow",
Plot:
"A team of explorers travel through a wormhole in space in an attempt to ensure humanity's survival.",
Language: "English",
Country: "USA, UK",
Awards: "Won 1 Oscar. Another 39 wins & 132 nominations.",
Poster:
"http://ia.media-imdb.com/images/M/MV5BMjIxNTU4MzY4MF5BMl5BanBnXkFtZTgwMzM4ODI3MjE@._V1_SX300.jpg",
Metascore: "74",
imdbRating: "8.6",
imdbVotes: "910,366",
imdbID: "tt0816692",
Type: "movie",
Response: "True"
},
{
Title: "The Dark Knight",
Year: "2008",
Rated: "PG-13",
Released: "18 Jul 2008",
Runtime: "152 min",
Genre: "Action, Adventure, Crime",
Director: "Christopher Nolan",
Writer:
"Jonathan Nolan (screenplay), Christopher Nolan (screenplay), Christopher Nolan (story), David S. Goyer (story), Bob Kane (characters)",
Actors: "Christian Bale, Heath Ledger, Aaron Eckhart, Michael Caine",
Plot:
"When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, the caped crusader must come to terms with one of the greatest psychological tests of his ability to fight injustice.",
Language: "English, Mandarin",
Country: "USA, UK",
Awards: "Won 2 Oscars. Another 146 wins & 142 nominations.",
Poster:
"http://ia.media-imdb.com/images/M/MV5BMTMxNTMwODM0NF5BMl5BanBnXkFtZTcwODAyMTk2Mw@@._V1_SX300.jpg",
Metascore: "82",
imdbRating: "9.0",
imdbVotes: "1,652,832",
imdbID: "tt0468569",
Type: "movie",
Response: "True"
},
{
Title: "Batman Begins",
Year: "2005",
Rated: "PG-13",
Released: "15 Jun 2005",
Runtime: "140 min",
Genre: "Action, Adventure",
Director: "Christopher Nolan",
Writer:
"Bob Kane (characters), David S. Goyer (story), Christopher Nolan (screenplay), David S. Goyer (screenplay)",
Actors: "Christian Bale, Michael Caine, Liam Neeson, Katie Holmes",
Plot: