Skip to content

Commit 86ac983

Browse files
committed
Functional Programming (ex. 6 to 12)
1 parent b0892d4 commit 86ac983

8 files changed

+445
-7
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Refactor Global Variables Out of Functions:
3+
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.
4+
5+
Note: Both functions should return an array, and any new parameters should be added before the bookName parameter.
6+
7+
- bookList should not change and still equal ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies",
8+
"Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"].
9+
- newBookList should equal ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies",
10+
"Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae", "A Brief History of Time"].
11+
- newerBookList should equal ["The Hound of the Baskervilles", "Philosophiæ Naturalis Principia Mathematica",
12+
"Disquisitiones Arithmeticae"].
13+
- newestBookList should equal ["The Hound of the Baskervilles", "Philosophiæ Naturalis Principia Mathematica",
14+
"Disquisitiones Arithmeticae", "A Brief History of Time"].
15+
*/
16+
// The global variable
17+
const bookList = [
18+
"The Hound of the Baskervilles",
19+
"On The Electrodynamics of Moving Bodies",
20+
"Philosophiæ Naturalis Principia Mathematica",
21+
"Disquisitiones Arithmeticae"
22+
];
23+
24+
// Changed code below this line
25+
function add(bookList, bookName) {
26+
let newBookList = [...bookList];
27+
28+
newBookList.push(bookName);
29+
30+
return newBookList;
31+
// Changed code above this line
32+
}
33+
34+
// Changed code below this line
35+
function remove(bookList, bookName) {
36+
let newBookList = [...bookList];
37+
const book_index = newBookList.indexOf(bookName);
38+
if (book_index >= 0) {
39+
40+
newBookList.splice(book_index, 1);
41+
return newBookList;
42+
43+
// Changed code above this line
44+
}
45+
}
46+
47+
const newBookList = add(bookList, 'A Brief History of Time');
48+
const newerBookList = remove(bookList, 'On The Electrodynamics of Moving Bodies');
49+
const newestBookList = remove(add(bookList, 'A Brief History of Time'), 'On The Electrodynamics of Moving Bodies');
50+
51+
console.log("bookList", bookList);
52+
console.log("newBookList", newBookList);
53+
console.log("newerBookList", newerBookList);
54+
console.log("newestBookList", newestBookList);
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
Use the map Method to Extract Data from an Array:
3+
The watchList array holds objects with information on several movies.
4+
Use map on watchList to assign a new array of objects to the ratings variable.
5+
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.
6+
The code in the editor currently uses a for loop to do this, so you should replace the loop functionality with your map expression.
7+
8+
- The watchList variable should not change.
9+
- Your code should not use a for loop.
10+
- Your code should use the map method.
11+
- ratings should equal [
12+
{"title": "Inception", "rating": "8.8"},
13+
{"title": "Interstellar", "rating": "8.6"},
14+
{"title": "The Dark Knight", "rating": "9.0"},
15+
{"title": "Batman Begins", "rating": "8.3"},
16+
{"title": "Avatar", "rating": "7.9"}].
17+
*/
18+
// The global variable
19+
const watchList = [
20+
{
21+
"Title": "Inception",
22+
"Year": "2010",
23+
"Rated": "PG-13",
24+
"Released": "16 Jul 2010",
25+
"Runtime": "148 min",
26+
"Genre": "Action, Adventure, Crime",
27+
"Director": "Christopher Nolan",
28+
"Writer": "Christopher Nolan",
29+
"Actors": "Leonardo DiCaprio, Joseph Gordon-Levitt, Elliot Page, Tom Hardy",
30+
"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.",
31+
"Language": "English, Japanese, French",
32+
"Country": "USA, UK",
33+
"Awards": "Won 4 Oscars. Another 143 wins & 198 nominations.",
34+
"Poster": "http://ia.media-imdb.com/images/M/MV5BMjAxMzY3NjcxNF5BMl5BanBnXkFtZTcwNTI5OTM0Mw@@._V1_SX300.jpg",
35+
"Metascore": "74",
36+
"imdbRating": "8.8",
37+
"imdbVotes": "1,446,708",
38+
"imdbID": "tt1375666",
39+
"Type": "movie",
40+
"Response": "True"
41+
},
42+
{
43+
"Title": "Interstellar",
44+
"Year": "2014",
45+
"Rated": "PG-13",
46+
"Released": "07 Nov 2014",
47+
"Runtime": "169 min",
48+
"Genre": "Adventure, Drama, Sci-Fi",
49+
"Director": "Christopher Nolan",
50+
"Writer": "Jonathan Nolan, Christopher Nolan",
51+
"Actors": "Ellen Burstyn, Matthew McConaughey, Mackenzie Foy, John Lithgow",
52+
"Plot": "A team of explorers travel through a wormhole in space in an attempt to ensure humanity's survival.",
53+
"Language": "English",
54+
"Country": "USA, UK",
55+
"Awards": "Won 1 Oscar. Another 39 wins & 132 nominations.",
56+
"Poster": "http://ia.media-imdb.com/images/M/MV5BMjIxNTU4MzY4MF5BMl5BanBnXkFtZTgwMzM4ODI3MjE@._V1_SX300.jpg",
57+
"Metascore": "74",
58+
"imdbRating": "8.6",
59+
"imdbVotes": "910,366",
60+
"imdbID": "tt0816692",
61+
"Type": "movie",
62+
"Response": "True"
63+
},
64+
{
65+
"Title": "The Dark Knight",
66+
"Year": "2008",
67+
"Rated": "PG-13",
68+
"Released": "18 Jul 2008",
69+
"Runtime": "152 min",
70+
"Genre": "Action, Adventure, Crime",
71+
"Director": "Christopher Nolan",
72+
"Writer": "Jonathan Nolan (screenplay), Christopher Nolan (screenplay), Christopher Nolan (story), David S. Goyer (story), Bob Kane (characters)",
73+
"Actors": "Christian Bale, Heath Ledger, Aaron Eckhart, Michael Caine",
74+
"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.",
75+
"Language": "English, Mandarin",
76+
"Country": "USA, UK",
77+
"Awards": "Won 2 Oscars. Another 146 wins & 142 nominations.",
78+
"Poster": "http://ia.media-imdb.com/images/M/MV5BMTMxNTMwODM0NF5BMl5BanBnXkFtZTcwODAyMTk2Mw@@._V1_SX300.jpg",
79+
"Metascore": "82",
80+
"imdbRating": "9.0",
81+
"imdbVotes": "1,652,832",
82+
"imdbID": "tt0468569",
83+
"Type": "movie",
84+
"Response": "True"
85+
},
86+
{
87+
"Title": "Batman Begins",
88+
"Year": "2005",
89+
"Rated": "PG-13",
90+
"Released": "15 Jun 2005",
91+
"Runtime": "140 min",
92+
"Genre": "Action, Adventure",
93+
"Director": "Christopher Nolan",
94+
"Writer": "Bob Kane (characters), David S. Goyer (story), Christopher Nolan (screenplay), David S. Goyer (screenplay)",
95+
"Actors": "Christian Bale, Michael Caine, Liam Neeson, Katie Holmes",
96+
"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.",
97+
"Language": "English, Urdu, Mandarin",
98+
"Country": "USA, UK",
99+
"Awards": "Nominated for 1 Oscar. Another 15 wins & 66 nominations.",
100+
"Poster": "http://ia.media-imdb.com/images/M/MV5BNTM3OTc0MzM2OV5BMl5BanBnXkFtZTYwNzUwMTI3._V1_SX300.jpg",
101+
"Metascore": "70",
102+
"imdbRating": "8.3",
103+
"imdbVotes": "972,584",
104+
"imdbID": "tt0372784",
105+
"Type": "movie",
106+
"Response": "True"
107+
},
108+
{
109+
"Title": "Avatar",
110+
"Year": "2009",
111+
"Rated": "PG-13",
112+
"Released": "18 Dec 2009",
113+
"Runtime": "162 min",
114+
"Genre": "Action, Adventure, Fantasy",
115+
"Director": "James Cameron",
116+
"Writer": "James Cameron",
117+
"Actors": "Sam Worthington, Zoe Saldana, Sigourney Weaver, Stephen Lang",
118+
"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.",
119+
"Language": "English, Spanish",
120+
"Country": "USA, UK",
121+
"Awards": "Won 3 Oscars. Another 80 wins & 121 nominations.",
122+
"Poster": "http://ia.media-imdb.com/images/M/MV5BMTYwOTEwNjAzMl5BMl5BanBnXkFtZTcwODc5MTUwMw@@._V1_SX300.jpg",
123+
"Metascore": "83",
124+
"imdbRating": "7.9",
125+
"imdbVotes": "876,575",
126+
"imdbID": "tt0499549",
127+
"Type": "movie",
128+
"Response": "True"
129+
}
130+
];
131+
132+
// Only changed code below this line
133+
134+
const ratings = watchList.map(
135+
movie => ({
136+
title: movie['Title'],
137+
rating: movie['imdbRating']
138+
}));
139+
140+
// Only changed code above this line
141+
142+
console.log(JSON.stringify(ratings));
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
Implement map on a Prototype:
3+
Write your own Array.prototype.myMap(), which should behave exactly like Array.prototype.map().
4+
You should not use the built-in map method. The Array instance can be accessed in the myMap method using this.
5+
6+
- new_s should equal [46, 130, 196, 10].
7+
- Your code should not use the map method.
8+
*/
9+
// The global variable
10+
const s = [23, 65, 98, 5];
11+
12+
Array.prototype.myMap = function (callback) {
13+
const newArray = [];
14+
// Only changed code below this line
15+
for (let element of this) {
16+
newArray.push(callback(element));
17+
}
18+
// Only changed code above this line
19+
return newArray;
20+
};
21+
22+
const new_s = s.myMap(function (item) {
23+
return item * 2;
24+
});
25+
26+
console.log(new_s);
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
Use the filter Method to Extract Data from an Array:
3+
The variable watchList holds an array of objects with information on several movies.
4+
Use a combination of filter and map on watchList to assign a new array of objects with only title and rating keys.
5+
The new array should only include objects where imdbRating is greater than or equal to 8.0.
6+
Note that the rating values are saved as strings in the object and you may need to convert them into numbers to perform
7+
mathematical operations on them.
8+
9+
- The watchList variable should not change.
10+
- Your code should use the filter method.
11+
- Your code should not use a for loop.
12+
- filteredList should equal
13+
[
14+
{"title": "Inception", "rating": "8.8"},
15+
{"title": "Interstellar", "rating": "8.6"},
16+
{"title": "The Dark Knight", "rating": "9.0"},
17+
{"title": "Batman Begins", "rating": "8.3"}
18+
].
19+
*/
20+
// The global variable
21+
const watchList = [
22+
{
23+
"Title": "Inception",
24+
"Year": "2010",
25+
"Rated": "PG-13",
26+
"Released": "16 Jul 2010",
27+
"Runtime": "148 min",
28+
"Genre": "Action, Adventure, Crime",
29+
"Director": "Christopher Nolan",
30+
"Writer": "Christopher Nolan",
31+
"Actors": "Leonardo DiCaprio, Joseph Gordon-Levitt, Elliot Page, Tom Hardy",
32+
"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.",
33+
"Language": "English, Japanese, French",
34+
"Country": "USA, UK",
35+
"Awards": "Won 4 Oscars. Another 143 wins & 198 nominations.",
36+
"Poster": "http://ia.media-imdb.com/images/M/MV5BMjAxMzY3NjcxNF5BMl5BanBnXkFtZTcwNTI5OTM0Mw@@._V1_SX300.jpg",
37+
"Metascore": "74",
38+
"imdbRating": "8.8",
39+
"imdbVotes": "1,446,708",
40+
"imdbID": "tt1375666",
41+
"Type": "movie",
42+
"Response": "True"
43+
},
44+
{
45+
"Title": "Interstellar",
46+
"Year": "2014",
47+
"Rated": "PG-13",
48+
"Released": "07 Nov 2014",
49+
"Runtime": "169 min",
50+
"Genre": "Adventure, Drama, Sci-Fi",
51+
"Director": "Christopher Nolan",
52+
"Writer": "Jonathan Nolan, Christopher Nolan",
53+
"Actors": "Ellen Burstyn, Matthew McConaughey, Mackenzie Foy, John Lithgow",
54+
"Plot": "A team of explorers travel through a wormhole in space in an attempt to ensure humanity's survival.",
55+
"Language": "English",
56+
"Country": "USA, UK",
57+
"Awards": "Won 1 Oscar. Another 39 wins & 132 nominations.",
58+
"Poster": "http://ia.media-imdb.com/images/M/MV5BMjIxNTU4MzY4MF5BMl5BanBnXkFtZTgwMzM4ODI3MjE@._V1_SX300.jpg",
59+
"Metascore": "74",
60+
"imdbRating": "8.6",
61+
"imdbVotes": "910,366",
62+
"imdbID": "tt0816692",
63+
"Type": "movie",
64+
"Response": "True"
65+
},
66+
{
67+
"Title": "The Dark Knight",
68+
"Year": "2008",
69+
"Rated": "PG-13",
70+
"Released": "18 Jul 2008",
71+
"Runtime": "152 min",
72+
"Genre": "Action, Adventure, Crime",
73+
"Director": "Christopher Nolan",
74+
"Writer": "Jonathan Nolan (screenplay), Christopher Nolan (screenplay), Christopher Nolan (story), David S. Goyer (story), Bob Kane (characters)",
75+
"Actors": "Christian Bale, Heath Ledger, Aaron Eckhart, Michael Caine",
76+
"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.",
77+
"Language": "English, Mandarin",
78+
"Country": "USA, UK",
79+
"Awards": "Won 2 Oscars. Another 146 wins & 142 nominations.",
80+
"Poster": "http://ia.media-imdb.com/images/M/MV5BMTMxNTMwODM0NF5BMl5BanBnXkFtZTcwODAyMTk2Mw@@._V1_SX300.jpg",
81+
"Metascore": "82",
82+
"imdbRating": "9.0",
83+
"imdbVotes": "1,652,832",
84+
"imdbID": "tt0468569",
85+
"Type": "movie",
86+
"Response": "True"
87+
},
88+
{
89+
"Title": "Batman Begins",
90+
"Year": "2005",
91+
"Rated": "PG-13",
92+
"Released": "15 Jun 2005",
93+
"Runtime": "140 min",
94+
"Genre": "Action, Adventure",
95+
"Director": "Christopher Nolan",
96+
"Writer": "Bob Kane (characters), David S. Goyer (story), Christopher Nolan (screenplay), David S. Goyer (screenplay)",
97+
"Actors": "Christian Bale, Michael Caine, Liam Neeson, Katie Holmes",
98+
"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.",
99+
"Language": "English, Urdu, Mandarin",
100+
"Country": "USA, UK",
101+
"Awards": "Nominated for 1 Oscar. Another 15 wins & 66 nominations.",
102+
"Poster": "http://ia.media-imdb.com/images/M/MV5BNTM3OTc0MzM2OV5BMl5BanBnXkFtZTYwNzUwMTI3._V1_SX300.jpg",
103+
"Metascore": "70",
104+
"imdbRating": "8.3",
105+
"imdbVotes": "972,584",
106+
"imdbID": "tt0372784",
107+
"Type": "movie",
108+
"Response": "True"
109+
},
110+
{
111+
"Title": "Avatar",
112+
"Year": "2009",
113+
"Rated": "PG-13",
114+
"Released": "18 Dec 2009",
115+
"Runtime": "162 min",
116+
"Genre": "Action, Adventure, Fantasy",
117+
"Director": "James Cameron",
118+
"Writer": "James Cameron",
119+
"Actors": "Sam Worthington, Zoe Saldana, Sigourney Weaver, Stephen Lang",
120+
"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.",
121+
"Language": "English, Spanish",
122+
"Country": "USA, UK",
123+
"Awards": "Won 3 Oscars. Another 80 wins & 121 nominations.",
124+
"Poster": "http://ia.media-imdb.com/images/M/MV5BMTYwOTEwNjAzMl5BMl5BanBnXkFtZTcwODc5MTUwMw@@._V1_SX300.jpg",
125+
"Metascore": "83",
126+
"imdbRating": "7.9",
127+
"imdbVotes": "876,575",
128+
"imdbID": "tt0499549",
129+
"Type": "movie",
130+
"Response": "True"
131+
}
132+
];
133+
134+
// Only changed code below this line
135+
136+
const filteredList = watchList
137+
.filter(movie => parseFloat(movie['imdbRating']) >= 8.0)
138+
.map(movie => (
139+
{
140+
title: movie['Title'],
141+
rating: movie['imdbRating']
142+
}));
143+
144+
// Only changed code above this line
145+
146+
console.log(filteredList);

0 commit comments

Comments
 (0)