Last active
July 22, 2017 07:39
-
-
Save yickling/82d15716e22a0e22534c21b9de54a6b3 to your computer and use it in GitHub Desktop.
http://reactivex.io/learnrx/ solutions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 1: Print all the names in an array | |
function(console) { | |
var names = ["Ben", "Jafar", "Matt", "Priya", "Brian"]; | |
names.map(function(name) { console.log(name); }); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 2: Use forEach to print all the names in an array | |
function(console) { | |
var names = ["Ben", "Jafar", "Matt", "Priya", "Brian"]; | |
names.forEach(function(name) { | |
console.log(name); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 3: Project an array of videos into an array of {id,title} pairs using forEach() | |
function() { | |
var newReleases = [ | |
{ | |
"id": 70111470, | |
"title": "Die Hard", | |
"boxart": "http://cdn-0.nflximg.com/images/2891/DieHard.jpg", | |
"uri": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": [4.0], | |
"bookmark": [] | |
}, | |
{ | |
"id": 654356453, | |
"title": "Bad Boys", | |
"boxart": "http://cdn-0.nflximg.com/images/2891/BadBoys.jpg", | |
"uri": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": [5.0], | |
"bookmark": [{ id: 432534, time: 65876586 }] | |
}, | |
{ | |
"id": 65432445, | |
"title": "The Chamber", | |
"boxart": "http://cdn-0.nflximg.com/images/2891/TheChamber.jpg", | |
"uri": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": [4.0], | |
"bookmark": [] | |
}, | |
{ | |
"id": 675465, | |
"title": "Fracture", | |
"boxart": "http://cdn-0.nflximg.com/images/2891/Fracture.jpg", | |
"uri": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": [5.0], | |
"bookmark": [{ id: 432534, time: 65876586 }] | |
} | |
], | |
videoAndTitlePairs = []; | |
// ------------ INSERT CODE HERE! ----------------------------------- | |
// Use forEach function to accumulate {id, title} pairs from each video. | |
// Put the results into the videoAndTitlePairs array using the Array's | |
// push() method. Example: videoAndTitlePairs.push(newItem); | |
// ------------ INSERT CODE HERE! ----------------------------------- | |
videoAndTitlePairs = newReleases.map((r) => { return { id: r.id, title: r.title }; }); | |
return videoAndTitlePairs; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 4: Implement map() | |
Array.prototype.map = function(projectionFunction) { | |
var results = []; | |
this.forEach(function(itemInArray) { | |
// ------------ INSERT CODE HERE! ---------------------------- | |
// Apply the projectionFunction to each item in the array and add | |
// each result to the results array. | |
// Note: you can add items to an array with the push() method. | |
// ------------ INSERT CODE HERE! ---------------------------- | |
results.push(projectionFunction(itemInArray)); | |
}); | |
return results; | |
}; | |
// JSON.stringify([1,2,3].map(function(x) { return x + 1; })) === '[2,3,4]' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 5: Use map() to project an array of videos into an array of {id,title} pairs | |
function() { | |
var newReleases = [ | |
{ | |
"id": 70111470, | |
"title": "Die Hard", | |
"boxart": "http://cdn-0.nflximg.com/images/2891/DieHard.jpg", | |
"uri": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": [4.0], | |
"bookmark": [] | |
}, | |
{ | |
"id": 654356453, | |
"title": "Bad Boys", | |
"boxart": "http://cdn-0.nflximg.com/images/2891/BadBoys.jpg", | |
"uri": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": [5.0], | |
"bookmark": [{ id: 432534, time: 65876586 }] | |
}, | |
{ | |
"id": 65432445, | |
"title": "The Chamber", | |
"boxart": "http://cdn-0.nflximg.com/images/2891/TheChamber.jpg", | |
"uri": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": [4.0], | |
"bookmark": [] | |
}, | |
{ | |
"id": 675465, | |
"title": "Fracture", | |
"boxart": "http://cdn-0.nflximg.com/images/2891/Fracture.jpg", | |
"uri": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": [5.0], | |
"bookmark": [{ id: 432534, time: 65876586 }] | |
} | |
]; | |
// ------------ INSERT CODE HERE! ----------------------------------- | |
// Use map function to accumulate {id, title} pairs from each video. | |
return newReleases.map(function (r) { return { id, title } = r; } ); // finish this expression! | |
// ------------ INSERT CODE HERE! ----------------------------------- | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 6: Use forEach() to collect only those videos with a rating of 5.0 | |
function() { | |
var newReleases = [ | |
{ | |
"id": 70111470, | |
"title": "Die Hard", | |
"boxart": "http://cdn-0.nflximg.com/images/2891/DieHard.jpg", | |
"uri": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 4.0, | |
"bookmark": [] | |
}, | |
{ | |
"id": 654356453, | |
"title": "Bad Boys", | |
"boxart": "http://cdn-0.nflximg.com/images/2891/BadBoys.jpg", | |
"uri": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 5.0, | |
"bookmark": [{ id: 432534, time: 65876586 }] | |
}, | |
{ | |
"id": 65432445, | |
"title": "The Chamber", | |
"boxart": "http://cdn-0.nflximg.com/images/2891/TheChamber.jpg", | |
"uri": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 4.0, | |
"bookmark": [] | |
}, | |
{ | |
"id": 675465, | |
"title": "Fracture", | |
"boxart": "http://cdn-0.nflximg.com/images/2891/Fracture.jpg", | |
"uri": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 5.0, | |
"bookmark": [{ id: 432534, time: 65876586 }] | |
} | |
], | |
videos = []; | |
// ------------ INSERT CODE HERE! ----------------------------------- | |
// Use forEach function to accumulate every video with a rating of 5.0 | |
// ------------ INSERT CODE HERE! ----------------------------------- | |
return newReleases.filter((r) => {return r.rating === 5.0}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 7: Implement filter() | |
Array.prototype.filter = function(predicateFunction) { | |
var results = []; | |
this.forEach(function(itemInArray) { | |
// ------------ INSERT CODE HERE! ---------------------------- | |
// Apply the predicateFunction to each item in the array. | |
// If the result is truthy, add the item to the results array. | |
// Note: remember you can add items to the array using the array's | |
// push() method. | |
// ------------ INSERT CODE HERE! ---------------------------- | |
if (predicateFunction(itemInArray)) results.push(itemInArray); | |
}); | |
return results; | |
}; | |
// JSON.stringify([1,2,3].filter(function(x) { return x > 2})) === "[3]" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 8: Chain filter and map to collect the ids of videos that have a rating of 5.0 | |
function() { | |
var newReleases = [ | |
{ | |
"id": 70111470, | |
"title": "Die Hard", | |
"boxart": "http://cdn-0.nflximg.com/images/2891/DieHard.jpg", | |
"uri": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 4.0, | |
"bookmark": [] | |
}, | |
{ | |
"id": 654356453, | |
"title": "Bad Boys", | |
"boxart": "http://cdn-0.nflximg.com/images/2891/BadBoys.jpg", | |
"uri": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 5.0, | |
"bookmark": [{ id: 432534, time: 65876586 }] | |
}, | |
{ | |
"id": 65432445, | |
"title": "The Chamber", | |
"boxart": "http://cdn-0.nflximg.com/images/2891/TheChamber.jpg", | |
"uri": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 4.0, | |
"bookmark": [] | |
}, | |
{ | |
"id": 675465, | |
"title": "Fracture", | |
"boxart": "http://cdn-0.nflximg.com/images/2891/Fracture.jpg", | |
"uri": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 5.0, | |
"bookmark": [{ id: 432534, time: 65876586 }] | |
} | |
]; | |
// ------------ INSERT CODE HERE! ----------------------------------- | |
// Chain the filter and map functions to select the id of all videos | |
// with a rating of 5.0. | |
return newReleases.filter((r) => r.rating === 5.0).map((r) => r.id) // Complete this expression | |
// ------------ INSERT CODE HERE! ----------------------------------- | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 9: Flatten the movieLists array into an array of video ids | |
function() { | |
var movieLists = [ | |
{ | |
name: "New Releases", | |
videos: [ | |
{ | |
"id": 70111470, | |
"title": "Die Hard", | |
"boxart": "http://cdn-0.nflximg.com/images/2891/DieHard.jpg", | |
"uri": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 4.0, | |
"bookmark": [] | |
}, | |
{ | |
"id": 654356453, | |
"title": "Bad Boys", | |
"boxart": "http://cdn-0.nflximg.com/images/2891/BadBoys.jpg", | |
"uri": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 5.0, | |
"bookmark": [{ id: 432534, time: 65876586 }] | |
} | |
] | |
}, | |
{ | |
name: "Dramas", | |
videos: [ | |
{ | |
"id": 65432445, | |
"title": "The Chamber", | |
"boxart": "http://cdn-0.nflximg.com/images/2891/TheChamber.jpg", | |
"uri": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 4.0, | |
"bookmark": [] | |
}, | |
{ | |
"id": 675465, | |
"title": "Fracture", | |
"boxart": "http://cdn-0.nflximg.com/images/2891/Fracture.jpg", | |
"uri": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 5.0, | |
"bookmark": [{ id: 432534, time: 65876586 }] | |
} | |
] | |
} | |
], | |
allVideoIdsInMovieLists = []; | |
// ------------ INSERT CODE HERE! ----------------------------------- | |
// Use two nested forEach loops to flatten the movieLists into a list of | |
// video ids. | |
// ------------ INSERT CODE HERE! ----------------------------------- | |
movieLists.forEach((list) => { list.videos.forEach((movie) => { | |
allVideoIdsInMovieLists.push(movie.id); }); }); | |
return allVideoIdsInMovieLists; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 10: Implement concatAll() | |
Array.prototype.concatAll = function() { | |
var results = []; | |
this.forEach(function(subArray) { | |
// ------------ INSERT CODE HERE! ---------------------------- | |
// Add all the items in each subArray to the results array. | |
// ------------ INSERT CODE HERE! ---------------------------- | |
subArray.forEach((item) => { results.push(item); }); | |
}); | |
return results; | |
}; | |
// JSON.stringify([ [1,2,3], [4,5,6], [7,8,9] ].concatAll()) === "[1,2,3,4,5,6,7,8,9]" | |
// [1,2,3].concatAll(); // throws an error because this is a one-dimensional array |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 11: Use map() and concatAll() to project and flatten the movieLists into an array of video ids | |
function() { | |
var movieLists = [ | |
{ | |
name: "New Releases", | |
videos: [ | |
{ | |
"id": 70111470, | |
"title": "Die Hard", | |
"boxart": "http://cdn-0.nflximg.com/images/2891/DieHard.jpg", | |
"uri": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 4.0, | |
"bookmark": [] | |
}, | |
{ | |
"id": 654356453, | |
"title": "Bad Boys", | |
"boxart": "http://cdn-0.nflximg.com/images/2891/BadBoys.jpg", | |
"uri": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 5.0, | |
"bookmark": [{ id: 432534, time: 65876586 }] | |
} | |
] | |
}, | |
{ | |
name: "Dramas", | |
videos: [ | |
{ | |
"id": 65432445, | |
"title": "The Chamber", | |
"boxart": "http://cdn-0.nflximg.com/images/2891/TheChamber.jpg", | |
"uri": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 4.0, | |
"bookmark": [] | |
}, | |
{ | |
"id": 675465, | |
"title": "Fracture", | |
"boxart": "http://cdn-0.nflximg.com/images/2891/Fracture.jpg", | |
"uri": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 5.0, | |
"bookmark": [{ id: 432534, time: 65876586 }] | |
} | |
] | |
} | |
]; | |
return movieLists. | |
map(function(movieList) { | |
return movieList.videos.map(function(video) { | |
return video.id; | |
}); | |
}). | |
concatAll(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 12: Retrieve id, title, and a 150x200 box art url for every video | |
function() { | |
var movieLists = [ | |
{ | |
name: "Instant Queue", | |
videos : [ | |
{ | |
"id": 70111470, | |
"title": "Die Hard", | |
"boxarts": [ | |
{ width: 150, height: 200, url: "http://cdn-0.nflximg.com/images/2891/DieHard150.jpg" }, | |
{ width: 200, height: 200, url: "http://cdn-0.nflximg.com/images/2891/DieHard200.jpg" } | |
], | |
"url": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 4.0, | |
"bookmark": [] | |
}, | |
{ | |
"id": 654356453, | |
"title": "Bad Boys", | |
"boxarts": [ | |
{ width: 200, height: 200, url: "http://cdn-0.nflximg.com/images/2891/BadBoys200.jpg" }, | |
{ width: 150, height: 200, url: "http://cdn-0.nflximg.com/images/2891/BadBoys150.jpg" } | |
], | |
"url": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 5.0, | |
"bookmark": [{ id: 432534, time: 65876586 }] | |
} | |
] | |
}, | |
{ | |
name: "New Releases", | |
videos: [ | |
{ | |
"id": 65432445, | |
"title": "The Chamber", | |
"boxarts": [ | |
{ width: 150, height: 200, url: "http://cdn-0.nflximg.com/images/2891/TheChamber150.jpg" }, | |
{ width: 200, height: 200, url: "http://cdn-0.nflximg.com/images/2891/TheChamber200.jpg" } | |
], | |
"url": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 4.0, | |
"bookmark": [] | |
}, | |
{ | |
"id": 675465, | |
"title": "Fracture", | |
"boxarts": [ | |
{ width: 200, height: 200, url: "http://cdn-0.nflximg.com/images/2891/Fracture200.jpg" }, | |
{ width: 150, height: 200, url: "http://cdn-0.nflximg.com/images/2891/Fracture150.jpg" }, | |
{ width: 300, height: 200, url: "http://cdn-0.nflximg.com/images/2891/Fracture300.jpg" } | |
], | |
"url": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 5.0, | |
"bookmark": [{ id: 432534, time: 65876586 }] | |
} | |
] | |
} | |
]; | |
return movieLists. | |
map(l => l.videos) | |
.concatAll() | |
.map(v => v.boxarts.map(b => { | |
return { id: v.id, title: v.title, boxart: b }; | |
}) | |
.filter(v => v.boxart.width === 150) | |
.map( v => { return { id: v.id, title: v.title, boxart: v.boxart.url }; })) | |
.concatAll(); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 13: Implement concatMap() | |
Array.prototype.concatMap = function(projectionFunctionThatReturnsArray) { | |
return this. | |
map(function(item) { | |
// ------------ INSERT CODE HERE! ---------------------------- | |
// Apply the projection function to each item. The projection | |
// function will return a new child array. This will create a | |
// two-dimensional array. | |
// ------------ INSERT CODE HERE! ---------------------------- | |
return projectionFunctionThatReturnsArray(item); | |
}). | |
// apply the concatAll function to flatten the two-dimensional array | |
concatAll(); | |
}; | |
/* | |
var spanishFrenchEnglishWords = [ ["cero","rien","zero"], ["uno","un","one"], ["dos","deux","two"] ]; | |
// collect all the words for each number, in every language, in a single, flat list | |
var allWords = [0,1,2]. | |
concatMap(function(index) { | |
return spanishFrenchEnglishWords[index]; | |
}); | |
return JSON.stringify(allWords) === '["cero","rien","zero","uno","un","one","dos","deux","two"]'; | |
*/ | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 14: Use concatMap() to retrieve id, title, and 150x200 box art url for every video | |
function() { | |
var movieLists = [ | |
{ | |
name: "Instant Queue", | |
videos : [ | |
{ | |
"id": 70111470, | |
"title": "Die Hard", | |
"boxarts": [ | |
{ width: 150, height: 200, url: "http://cdn-0.nflximg.com/images/2891/DieHard150.jpg" }, | |
{ width: 200, height: 200, url: "http://cdn-0.nflximg.com/images/2891/DieHard200.jpg" } | |
], | |
"url": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 4.0, | |
"bookmark": [] | |
}, | |
{ | |
"id": 654356453, | |
"title": "Bad Boys", | |
"boxarts": [ | |
{ width: 200, height: 200, url: "http://cdn-0.nflximg.com/images/2891/BadBoys200.jpg" }, | |
{ width: 150, height: 200, url: "http://cdn-0.nflximg.com/images/2891/BadBoys150.jpg" } | |
], | |
"url": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 5.0, | |
"bookmark": [{ id: 432534, time: 65876586 }] | |
} | |
] | |
}, | |
{ | |
name: "New Releases", | |
videos: [ | |
{ | |
"id": 65432445, | |
"title": "The Chamber", | |
"boxarts": [ | |
{ width: 150, height: 200, url: "http://cdn-0.nflximg.com/images/2891/TheChamber150.jpg" }, | |
{ width: 200, height: 200, url: "http://cdn-0.nflximg.com/images/2891/TheChamber200.jpg" } | |
], | |
"url": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 4.0, | |
"bookmark": [] | |
}, | |
{ | |
"id": 675465, | |
"title": "Fracture", | |
"boxarts": [ | |
{ width: 200, height: 200, url: "http://cdn-0.nflximg.com/images/2891/Fracture200.jpg" }, | |
{ width: 150, height: 200, url: "http://cdn-0.nflximg.com/images/2891/Fracture150.jpg" }, | |
{ width: 300, height: 200, url: "http://cdn-0.nflximg.com/images/2891/Fracture300.jpg" } | |
], | |
"url": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 5.0, | |
"bookmark": [{ id: 432534, time: 65876586 }] | |
} | |
] | |
} | |
]; | |
// Use one or more concatMap, map, and filter calls to create an array with the following items | |
// [ | |
// {"id": 675465, "title": "Fracture", "boxart": "http://cdn-0.nflximg.com/images/2891/Fracture150.jpg" }, | |
// {"id": 65432445, "title": "The Chamber", "boxart": "http://cdn-0.nflximg.com/images/2891/TheChamber150.jpg" }, | |
// {"id": 654356453, "title": "Bad Boys", "boxart": "http://cdn-0.nflximg.com/images/2891/BadBoys150.jpg" }, | |
// {"id": 70111470, "title": "Die Hard", "boxart": "http://cdn-0.nflximg.com/images/2891/DieHard150.jpg" } | |
// ]; | |
return movieLists.concatMap(l => l.videos) | |
.concatMap(v => v.boxarts.filter(b => b.width === 150) | |
.map(a => { return { id: v.id, title: v.title, boxart: a.url } })) | |
// Complete this expression! | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 15: Use forEach to find the largest box art | |
function() { | |
var boxarts = [ | |
{ width: 200, height: 200, url: "http://cdn-0.nflximg.com/images/2891/Fracture200.jpg" }, | |
{ width: 150, height: 200, url: "http://cdn-0.nflximg.com/images/2891/Fracture150.jpg" }, | |
{ width: 300, height: 200, url: "http://cdn-0.nflximg.com/images/2891/Fracture300.jpg" }, | |
{ width: 425, height: 150, url: "http://cdn-0.nflximg.com/images/2891/Fracture425.jpg" } | |
], | |
currentSize, | |
maxSize = -1, | |
largestBoxart; | |
boxarts.forEach(function(boxart) { | |
currentSize = boxart.width * boxart.height; | |
if (currentSize > maxSize) { | |
largestBoxart = boxart; | |
maxSize = currentSize; | |
} | |
}); | |
return largestBoxart; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 16: Implement reduce() | |
// [1,2,3].reduce(function(accumulatedValue, currentValue) { return accumulatedValue + currentValue; }); === [6]; | |
// [1,2,3].reduce(function(accumulatedValue, currentValue) { return accumulatedValue + currentValue; }, 10); === [16]; | |
Array.prototype.reduce = function(combiner, initialValue) { | |
var counter, | |
accumulatedValue; | |
// If the array is empty, do nothing | |
if (this.length === 0) { | |
return this; | |
} | |
else { | |
// If the user didn't pass an initial value, use the first item. | |
if (arguments.length === 1) { | |
counter = 1; | |
accumulatedValue = this[0]; | |
} | |
else if (arguments.length >= 2) { | |
counter = 0; | |
accumulatedValue = initialValue; | |
} | |
else { | |
throw "Invalid arguments."; | |
} | |
// Loop through the array, feeding the current value and the result of | |
// the previous computation back into the combiner function until | |
// we've exhausted the entire array and are left with only one value. | |
while(counter < this.length) { | |
accumulatedValue = combiner(accumulatedValue, this[counter]) | |
counter++; | |
} | |
return [accumulatedValue]; | |
} | |
}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 17: Retrieve the largest rating. | |
function() { | |
var ratings = [2,3,1,4,5]; | |
// You should return an array containing only the largest rating. Remember that reduce always | |
// returns an array with one item. | |
return ratings. | |
reduce((a,b) => (b > a) ? b : a) // Complete this expression | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 18: Retrieve url of the largest boxart | |
function() { | |
var boxarts = [ | |
{ width: 200, height: 200, url: "http://cdn-0.nflximg.com/images/2891/Fracture200.jpg" }, | |
{ width: 150, height: 200, url: "http://cdn-0.nflximg.com/images/2891/Fracture150.jpg" }, | |
{ width: 300, height: 200, url: "http://cdn-0.nflximg.com/images/2891/Fracture300.jpg" }, | |
{ width: 425, height: 150, url: "http://cdn-0.nflximg.com/images/2891/Fracture425.jpg" } | |
]; | |
// You should return an array containing only the URL of the largest box art. Remember that reduce always | |
// returns an array with one item. | |
return boxarts. | |
reduce((a, b) => (b.width > a.width) ? b : a) | |
.map(b => b.url)// Complete this expression | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 19: Reducing with an initial value | |
function() { | |
var videos = [ | |
{ | |
"id": 65432445, | |
"title": "The Chamber" | |
}, | |
{ | |
"id": 675465, | |
"title": "Fracture" | |
}, | |
{ | |
"id": 70111470, | |
"title": "Die Hard" | |
}, | |
{ | |
"id": 654356453, | |
"title": "Bad Boys" | |
} | |
]; | |
// Expecting this output... | |
// [ | |
// { | |
// "65432445": "The Chamber", | |
// "675465": "Fracture", | |
// "70111470": "Die Hard", | |
// "654356453": "Bad Boys" | |
// } | |
// ] | |
return videos. | |
reduce(function(accumulatedMap, video) { | |
var obj = {}; | |
// ----- INSERT CODE TO ADD THE VIDEO TITLE TO THE ---- | |
// ----- NEW MAP USING THE VIDEO ID AS THE KEY ---- | |
obj[video.id] = video.title; | |
// Object.assign() takes all of the enumerable properties from | |
// the object listed in its second argument (obj) and assigns them | |
// to the object listed in its first argument (accumulatedMap). | |
return Object.assign(accumulatedMap, obj); | |
}, | |
// Use an empty map as the initial value instead of the first item in | |
// the list. | |
{}); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 20: Retrieve the id, title, and smallest box art url for every video. | |
function() { | |
var movieLists = [ | |
{ | |
name: "New Releases", | |
videos: [ | |
{ | |
"id": 70111470, | |
"title": "Die Hard", | |
"boxarts": [ | |
{ width: 150, height:200, url:"http://cdn-0.nflximg.com/images/2891/DieHard150.jpg" }, | |
{ width: 200, height:200, url:"http://cdn-0.nflximg.com/images/2891/DieHard200.jpg" } | |
], | |
"url": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 4.0, | |
"bookmark": [] | |
}, | |
{ | |
"id": 654356453, | |
"title": "Bad Boys", | |
"boxarts": [ | |
{ width: 200, height:200, url:"http://cdn-0.nflximg.com/images/2891/BadBoys200.jpg" }, | |
{ width: 140, height:200, url:"http://cdn-0.nflximg.com/images/2891/BadBoys140.jpg" } | |
], | |
"url": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 5.0, | |
"bookmark": [{ id:432534, time:65876586 }] | |
} | |
] | |
}, | |
{ | |
name: "Thrillers", | |
videos: [ | |
{ | |
"id": 65432445, | |
"title": "The Chamber", | |
"boxarts": [ | |
{ width: 130, height:200, url:"http://cdn-0.nflximg.com/images/2891/TheChamber130.jpg" }, | |
{ width: 200, height:200, url:"http://cdn-0.nflximg.com/images/2891/TheChamber200.jpg" } | |
], | |
"url": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 4.0, | |
"bookmark": [] | |
}, | |
{ | |
"id": 675465, | |
"title": "Fracture", | |
"boxarts": [ | |
{ width: 200, height:200, url:"http://cdn-0.nflximg.com/images/2891/Fracture200.jpg" }, | |
{ width: 120, height:200, url:"http://cdn-0.nflximg.com/images/2891/Fracture120.jpg" }, | |
{ width: 300, height:200, url:"http://cdn-0.nflximg.com/images/2891/Fracture300.jpg" } | |
], | |
"url": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 5.0, | |
"bookmark": [{ id:432534, time:65876586 }] | |
} | |
] | |
} | |
]; | |
// Use one or more concatMap, map, and reduce calls to create an array with the following items (order doesn't matter) | |
// [ | |
// {"id": 675465,"title": "Fracture","boxart":"http://cdn-0.nflximg.com/images/2891/Fracture120.jpg" }, | |
// {"id": 65432445,"title": "The Chamber","boxart":"http://cdn-0.nflximg.com/images/2891/TheChamber130.jpg" }, | |
// {"id": 654356453,"title": "Bad Boys","boxart":"http://cdn-0.nflximg.com/images/2891/BadBoys140.jpg" }, | |
// {"id": 70111470,"title": "Die Hard","boxart":"http://cdn-0.nflximg.com/images/2891/DieHard150.jpg" } | |
// ]; | |
return movieLists. | |
concatMap((movieList) => movieList.videos) | |
.concatMap((v) => v.boxarts.reduce((a,b) => (b.width < a.width) ? b : a) | |
.map(b => { return { id: v.id, title: v.title, boxart: b.url }; })) | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 21: Combine videos and bookmarks by index | |
function() { | |
var videos = [ | |
{ | |
"id": 70111470, | |
"title": "Die Hard", | |
"boxart": "http://cdn-0.nflximg.com/images/2891/DieHard.jpg", | |
"uri": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 4.0, | |
}, | |
{ | |
"id": 654356453, | |
"title": "Bad Boys", | |
"boxart": "http://cdn-0.nflximg.com/images/2891/BadBoys.jpg", | |
"uri": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 5.0, | |
}, | |
{ | |
"id": 65432445, | |
"title": "The Chamber", | |
"boxart": "http://cdn-0.nflximg.com/images/2891/TheChamber.jpg", | |
"uri": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 4.0, | |
}, | |
{ | |
"id": 675465, | |
"title": "Fracture", | |
"boxart": "http://cdn-0.nflximg.com/images/2891/Fracture.jpg", | |
"uri": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 5.0, | |
} | |
], | |
bookmarks = [ | |
{id: 470, time: 23432}, | |
{id: 453, time: 234324}, | |
{id: 445, time: 987834} | |
], | |
counter, | |
videoIdAndBookmarkIdPairs = []; | |
for(counter = 0; counter < Math.min(videos.length, bookmarks.length); counter++) { | |
// Insert code here to create a {videoId, bookmarkId} pair and add it to the videoIdAndBookmarkIdPairs array. | |
videoIdAndBookmarkIdPairs.push({ videoId: videos[counter].id, bookmarkId: bookmarks[counter].id }); | |
} | |
return videoIdAndBookmarkIdPairs; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 22: Implement zip | |
// JSON.stringify(Array.zip([1,2,3],[4,5,6], function(left, right) { return left + right })) === '[5,7,9]' | |
Array.zip = function(left, right, combinerFunction) { | |
var counter, | |
results = []; | |
for(counter = 0; counter < Math.min(left.length, right.length); counter++) { | |
// Add code here to apply the combinerFunction to the left and right-hand items in the respective arrays | |
results.push(combinerFunction(left[counter], right[counter])) | |
} | |
return results; | |
}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 23: Combine videos and bookmarks by index | |
function() { | |
var videos = [ | |
{ | |
"id": 70111470, | |
"title": "Die Hard", | |
"boxart": "http://cdn-0.nflximg.com/images/2891/DieHard.jpg", | |
"uri": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 4.0, | |
}, | |
{ | |
"id": 654356453, | |
"title": "Bad Boys", | |
"boxart": "http://cdn-0.nflximg.com/images/2891/BadBoys.jpg", | |
"uri": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 5.0, | |
}, | |
{ | |
"id": 65432445, | |
"title": "The Chamber", | |
"boxart": "http://cdn-0.nflximg.com/images/2891/TheChamber.jpg", | |
"uri": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 4.0, | |
}, | |
{ | |
"id": 675465, | |
"title": "Fracture", | |
"boxart": "http://cdn-0.nflximg.com/images/2891/Fracture.jpg", | |
"uri": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 5.0, | |
} | |
], | |
bookmarks = [ | |
{id: 470, time: 23432}, | |
{id: 453, time: 234324}, | |
{id: 445, time: 987834} | |
]; | |
return Array. | |
zip(videos, bookmarks, (v, b) => { return { videoId: v.id, bookmarkId: b.id }; }) //... finish this expression | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 24: Retrieve each video's id, title, middle interesting moment time, and smallest box art url. | |
function() { | |
var movieLists = [ | |
{ | |
name: "New Releases", | |
videos: [ | |
{ | |
"id": 70111470, | |
"title": "Die Hard", | |
"boxarts": [ | |
{ width: 150, height:200, url:"http://cdn-0.nflximg.com/images/2891/DieHard150.jpg" }, | |
{ width: 200, height:200, url:"http://cdn-0.nflximg.com/images/2891/DieHard200.jpg" } | |
], | |
"url": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 4.0, | |
"interestingMoments": [ | |
{ type: "End", time:213432 }, | |
{ type: "Start", time: 64534 }, | |
{ type: "Middle", time: 323133} | |
] | |
}, | |
{ | |
"id": 654356453, | |
"title": "Bad Boys", | |
"boxarts": [ | |
{ width: 200, height:200, url:"http://cdn-0.nflximg.com/images/2891/BadBoys200.jpg" }, | |
{ width: 140, height:200, url:"http://cdn-0.nflximg.com/images/2891/BadBoys140.jpg" } | |
], | |
"url": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 5.0, | |
"interestingMoments": [ | |
{ type: "End", time:54654754 }, | |
{ type: "Start", time: 43524243 }, | |
{ type: "Middle", time: 6575665} | |
] | |
} | |
] | |
}, | |
{ | |
name: "Instant Queue", | |
videos: [ | |
{ | |
"id": 65432445, | |
"title": "The Chamber", | |
"boxarts": [ | |
{ width: 130, height:200, url:"http://cdn-0.nflximg.com/images/2891/TheChamber130.jpg" }, | |
{ width: 200, height:200, url:"http://cdn-0.nflximg.com/images/2891/TheChamber200.jpg" } | |
], | |
"url": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 4.0, | |
"interestingMoments": [ | |
{ type: "End", time:132423 }, | |
{ type: "Start", time: 54637425 }, | |
{ type: "Middle", time: 3452343} | |
] | |
}, | |
{ | |
"id": 675465, | |
"title": "Fracture", | |
"boxarts": [ | |
{ width: 200, height:200, url:"http://cdn-0.nflximg.com/images/2891/Fracture200.jpg" }, | |
{ width: 120, height:200, url:"http://cdn-0.nflximg.com/images/2891/Fracture120.jpg" }, | |
{ width: 300, height:200, url:"http://cdn-0.nflximg.com/images/2891/Fracture300.jpg" } | |
], | |
"url": "http://api.netflix.com/catalog/titles/movies/70111470", | |
"rating": 5.0, | |
"interestingMoments": [ | |
{ type: "End", time:45632456 }, | |
{ type: "Start", time: 234534 }, | |
{ type: "Middle", time: 3453434} | |
] | |
} | |
] | |
} | |
]; | |
//------------ COMPLETE THIS EXPRESSION -------------- | |
return movieLists. | |
concatMap((movieList) => movieList.videos) | |
.concatMap((v) => Array.zip(v.interestingMoments.filter(a => a.type === 'Middle'), | |
v.boxarts.reduce((a,b) => b.width < a.width ? b : a), | |
(m, b) => { return { id: v.id, title: v.title, time: m.time, | |
url: b.url }; } )); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 25: Converting from Arrays to Trees | |
function() { | |
var lists = [ | |
{ | |
"id": 5434364, | |
"name": "New Releases" | |
}, | |
{ | |
"id": 65456475, | |
"name": "Thrillers" | |
} | |
], | |
videos = [ | |
{ | |
"listId": 5434364, | |
"id": 65432445, | |
"title": "The Chamber" | |
}, | |
{ | |
"listId": 5434364, | |
"id": 675465, | |
"title": "Fracture" | |
}, | |
{ | |
"listId": 65456475, | |
"id": 70111470, | |
"title": "Die Hard" | |
}, | |
{ | |
"listId": 65456475, | |
"id": 654356453, | |
"title": "Bad Boys" | |
} | |
]; | |
return lists.map(c => { return { name: c.name, videos: videos | |
.filter(v => v.listId === c.id).map(v => { return { id: v.id, title: v.title }; })} }) // complete this expression | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 26: Converting from Arrays to Deeper Trees | |
function() { | |
var lists = [ | |
{ | |
"id": 5434364, | |
"name": "New Releases" | |
}, | |
{ | |
"id": 65456475, | |
name: "Thrillers" | |
} | |
], | |
videos = [ | |
{ | |
"listId": 5434364, | |
"id": 65432445, | |
"title": "The Chamber" | |
}, | |
{ | |
"listId": 5434364, | |
"id": 675465, | |
"title": "Fracture" | |
}, | |
{ | |
"listId": 65456475, | |
"id": 70111470, | |
"title": "Die Hard" | |
}, | |
{ | |
"listId": 65456475, | |
"id": 654356453, | |
"title": "Bad Boys" | |
} | |
], | |
boxarts = [ | |
{ videoId: 65432445, width: 130, height:200, url:"http://cdn-0.nflximg.com/images/2891/TheChamber130.jpg" }, | |
{ videoId: 65432445, width: 200, height:200, url:"http://cdn-0.nflximg.com/images/2891/TheChamber200.jpg" }, | |
{ videoId: 675465, width: 200, height:200, url:"http://cdn-0.nflximg.com/images/2891/Fracture200.jpg" }, | |
{ videoId: 675465, width: 120, height:200, url:"http://cdn-0.nflximg.com/images/2891/Fracture120.jpg" }, | |
{ videoId: 675465, width: 300, height:200, url:"http://cdn-0.nflximg.com/images/2891/Fracture300.jpg" }, | |
{ videoId: 70111470, width: 150, height:200, url:"http://cdn-0.nflximg.com/images/2891/DieHard150.jpg" }, | |
{ videoId: 70111470, width: 200, height:200, url:"http://cdn-0.nflximg.com/images/2891/DieHard200.jpg" }, | |
{ videoId: 654356453, width: 200, height:200, url:"http://cdn-0.nflximg.com/images/2891/BadBoys200.jpg" }, | |
{ videoId: 654356453, width: 140, height:200, url:"http://cdn-0.nflximg.com/images/2891/BadBoys140.jpg" } | |
], | |
bookmarks = [ | |
{ videoId: 65432445, time: 32432 }, | |
{ videoId: 675465, time: 3534543 }, | |
{ videoId: 70111470, time: 645243 }, | |
{ videoId: 654356453, time: 984934 } | |
]; | |
return lists.map((c) => | |
{ return { name: c.name, videos: | |
videos.filter(g => g.listId === c.id).concatMap(v => Array.zip( | |
boxarts.filter(a => a.videoId === v.id).reduce((a, b) => b.width < a.width ? b : a), | |
bookmarks.filter(a => a.videoId === v.id), | |
(arts, book) => { return { id: v.id, title: v.title, | |
time: book.time, boxart: arts.url }; } )) } }) | |
// complete this expression | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 27: Stock Ticker | |
function(pricesNASDAQ, printRecord) { | |
var microsoftPrices, | |
now = new Date(), | |
tenDaysAgo = new Date( now.getFullYear(), now.getMonth(), now.getDate() - 10); | |
// use filter() to filter the trades for MSFT prices recorded any time after 10 days ago | |
microsoftPrices = | |
pricesNASDAQ. | |
filter(function(priceRecord) { // finish this expression | |
return priceRecord.name === 'MSFT' }) | |
// Print the trades to the output console | |
microsoftPrices. | |
forEach(function(priceRecord) { | |
printRecord(priceRecord); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 28: Subscribing to an event | |
function(button) { | |
// the button click handler | |
var handler = function(ev) { | |
// Unsubscribe from the button event. | |
button.removeEventListener("click", handler); | |
alert("Button was clicked. Unsubscribing from event."); | |
}; | |
// add the button click handler | |
button.addEventListener("click", handler); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 29: Traversing an Event | |
function(button) { | |
var buttonClicks = Observable.fromEvent(button, "click"); | |
// In the case of an Observable, forEach returns a subscription object. | |
var subscription = | |
buttonClicks. | |
forEach(function(clickEvent) { | |
alert("Button was clicked. Stopping Traversal."); | |
// Stop traversing the button clicks | |
subscription.dispose(); | |
}); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 30: Completing Sequences with take() | |
function(button) { | |
var buttonClicks = Observable.fromEvent(button, "click"); | |
// Use take() to listen for only one button click | |
// and unsubscribe. | |
buttonClicks. | |
take(1). | |
forEach(function() { | |
alert("Button was clicked once. Stopping Traversal."); | |
}); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 31: Completing sequences with takeUntil() | |
function(pricesNASDAQ, printRecord, stopButton) { | |
var stopButtonClicks = Observable.fromEvent(stopButton, "click");// ----- To finish this expression, use Observable.fromEvent to convert the "click" event on the stop button to an Observable | |
microsoftPrices = | |
pricesNASDAQ. | |
filter(function(priceRecord) { | |
return priceRecord.name === "MSFT"; | |
}).takeUntil(stopButtonClicks) | |
// ----- To finish this expression, use takeUntil to complete the sequence when stopButtonClicks fires. | |
microsoftPrices. | |
forEach(function(priceRecord) { | |
printRecord(priceRecord); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 32: Creating a mouse drag event | |
function(sprite, spriteContainer) { | |
var spriteMouseDowns = Observable.fromEvent(sprite, "mousedown"), | |
spriteContainerMouseMoves = Observable.fromEvent(spriteContainer, "mousemove"), | |
spriteContainerMouseUps = Observable.fromEvent(spriteContainer, "mouseup"), | |
spriteMouseDrags = | |
// For every mouse down event on the sprite... | |
spriteMouseDowns. | |
concatMap((contactPoint) => spriteContainerMouseMoves. | |
// ...until a mouse up event occurs. | |
takeUntil(spriteContainerMouseUps) | |
); | |
// For each mouse drag event, move the sprite to the absolute page position. | |
spriteMouseDrags.forEach(function(dragPoint) { | |
sprite.style.left = dragPoint.pageX + "px"; | |
sprite.style.top = dragPoint.pageY + "px"; | |
}); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 33: Improving our mouse drag event | |
function(sprite, spriteContainer) { | |
// All of the mouse event sequences look like this: | |
// seq([ {pageX: 22, pageY: 3423, layerX: 14, layerY: 22} ,,, ]) | |
var spriteMouseDowns = Observable.fromEvent(sprite, "mousedown"), | |
spriteContainerMouseMoves = Observable.fromEvent(spriteContainer, "mousemove"), | |
spriteContainerMouseUps = Observable.fromEvent(spriteContainer, "mouseup"), | |
// Create a sequence that looks like this: | |
// seq([ {pageX: 22, pageY:4080 },,,{pageX: 24, pageY: 4082},,, ]) | |
spriteMouseDrags = | |
// For every mouse down event on the sprite... | |
spriteMouseDowns. | |
concatMap(function(contactPoint) { | |
// ...retrieve all the mouse move events on the sprite container... | |
return spriteContainerMouseMoves. | |
// ...until a mouse up event occurs. | |
takeUntil(spriteContainerMouseUps). | |
map((c) => { | |
return { | |
pageX: c.pageX - contactPoint.layerX, | |
pageY: c.pageY - contactPoint.layerY | |
}; | |
}); | |
}); | |
// For each mouse drag event, move the sprite to the absolute page position. | |
spriteMouseDrags.forEach(function(dragPoint) { | |
sprite.style.left = dragPoint.pageX + "px"; | |
sprite.style.top = dragPoint.pageY + "px"; | |
}); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 34: HTTP requests | |
function($) { | |
$.getJSON( | |
"http://api-global.netflix.com/queue", | |
{ | |
success: function(json) { | |
alert("Data has arrived."); | |
}, | |
error: function(ex) { | |
alert("There was an error.") | |
} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 35: Sequencing HTTP requests with callbacks | |
function(window, $, showMovieLists, showError) { | |
var error, | |
configDone, | |
movieLists, | |
queueList, | |
windowLoaded, | |
outputDisplayed, | |
errorHandler = function() { | |
// Otherwise show the error. | |
error = "There was a connectivity error"; | |
// We may be ready to display out | |
tryToDisplayOutput(); | |
}, | |
tryToDisplayOutput = function() { | |
if (outputDisplayed) { | |
return; | |
} | |
if (windowLoaded) { | |
if (configDone && movieLists !== undefined) { | |
if (queueList !== undefined) { | |
movieLists.push(queueList); | |
} | |
outputDisplayed = true; | |
showMovieLists(JSON.stringify(movieLists)); | |
} | |
else if (error) { | |
outputDisplayed = true; | |
showError(error); | |
} | |
} | |
}, | |
windowLoadHandler = function() { | |
windowLoaded = true; | |
// Remember to unsubscribe from events | |
window.removeEventListener("load", windowLoadHandler); | |
// This may be the last task we're waiting on, so try and display output. | |
tryToDisplayOutput(); | |
}; | |
// Register for the load event | |
window.addEventListener("load", windowLoadHandler); | |
// Request the service url prefix for the users AB test | |
$.getJSON( | |
"http://api-global.netflix.com/abTestInformation", | |
{ | |
success: function(abTestInformation) { | |
// Request the member's config information to determine whether their instant | |
// queue should be displayed. | |
$.getJSON( | |
"http://api-global.netflix.com/" + abTestInformation.urlPrefix + "/config", | |
{ | |
success: function(config) { | |
// Parallel retrieval of movie list could've failed, | |
// in which case we don't want to issue this call. | |
if (!error) { | |
// If we're supposed to | |
if (config.showInstantQueue) { | |
$.getJSON( | |
"http://api-global.netflix.com/" + abTestInformation.urlPrefix + "/queue", | |
{ | |
success: function(queueMessage) { | |
queueList = queueMessage.list; | |
configDone = true; | |
tryToDisplayOutput(); | |
}, | |
error: errorHandler | |
}); | |
} | |
else { | |
configDone = true; | |
tryToDisplayOutput(); | |
} | |
} | |
}, | |
error: errorHandler | |
}); | |
// Retrieve the movie list | |
$.getJSON( | |
"http://api-global.netflix.com/" + abTestInformation.urlPrefix + "/movieLists", | |
{ | |
success: function(movieListMessage) { | |
movieLists = movieListMessage.list; | |
tryToDisplayOutput(); | |
}, | |
error: errorHandler | |
}); | |
}, | |
error: errorHandler | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 36: Traversing callback-based Asynchronous APIs | |
function(window, $) { | |
var getJSON = function(url) { | |
return Observable.create(function(observer) { | |
var subscribed = true; | |
$.getJSON(url, | |
{ | |
success: | |
function(data) { | |
// If client is still interested in the results, send them. | |
if (subscribed) { | |
// Send data to the client | |
observer.next(data); | |
// Immediately complete the sequence | |
observer.complete(); | |
} | |
}, | |
error: function(ex) { | |
// If client is still interested in the results, send them. | |
if (subscribed) { | |
// Inform the client that an error occurred. | |
observer.error(ex); | |
} | |
} | |
}); | |
// Definition of the Subscription objects unsubscribe (dispose in RxJS 4) method. | |
return function() { | |
subscribed = false; | |
} | |
}); | |
}; | |
var observer = { | |
// onNext in RxJS 4 | |
next: function (data) { | |
alert(JSON.stringify(data)); | |
}, | |
// onError in RxJS 4 | |
error: function (err) { | |
alert(err); | |
}, | |
// onComplete in RxJS 4 | |
complete: function () { | |
alert("The asynchronous operation has completed."); | |
} | |
}; | |
var subscription = | |
getJSON("http://api-global.netflix.com/abTestInformation").subscribe(observer); | |
// setTimeout(function () { | |
// alert("Changed my mind, I do not want notifications any more!") | |
// subscription.unsubscribe(); | |
// }, 10); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 37: Sequencing HTTP requests with Observable | |
function(window, getJSON, showMovieLists, showError) { | |
var movieListsSequence = | |
Observable.zip( | |
getJSON("http://api-global.netflix.com/abTestInformation"). | |
concatMap(function(abTestInformation) { | |
return Observable.zip( | |
getJSON("http://api-global.netflix.com/" + abTestInformation.urlPrefix + "/config"). | |
concatMap(function(config) { | |
if (config.showInstantQueue) { | |
return getJSON("http://api-global.netflix.com/" + abTestInformation.urlPrefix + "/queue"). | |
map(function(queueMessage) { | |
return queueMessage.list; | |
}); | |
} | |
else { | |
return Observable.returnValue(undefined); | |
} | |
}), | |
getJSON("http://api-global.netflix.com/" + abTestInformation.urlPrefix + "/movieLists"), | |
function(queue, movieListsMessage) { | |
var copyOfMovieLists = Object.create(movieListsMessage.list); | |
if (queue !== undefined) { | |
copyOfMovieLists.push(queue); | |
} | |
return copyOfMovieLists; | |
}); | |
}), | |
Observable.fromEvent(window, "load"), | |
function(movieLists, loadEvent) { | |
return movieLists; | |
}); | |
movieListsSequence. | |
forEach( | |
function(movieLists) { | |
showMovieLists(movieLists); | |
}, | |
function(err) { | |
showError(err); | |
}); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 38: Throttle Input | |
function (clicks, saveData, name) { | |
return clicks. | |
throttleTime(1000). | |
concatMap(function () { | |
return saveData(name); | |
}) | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 39: Autocomplete Box | |
function (getSearchResultSet, keyPresses, textBox) { | |
var getSearchResultSets = | |
keyPresses. | |
map(function () { | |
return textBox.value; | |
}). | |
throttleTime(1000). | |
// TODO: Ensure that we only trigger a maximum of one search request per second | |
concatMap(function (text) { | |
// TODO: Ensure this sequence ends as soon as another key press arrives | |
return getSearchResultSet(text).takeUntil(keyPresses); | |
}); | |
return getSearchResultSets; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 40: Distinct Until Changed Input | |
function (keyPresses, isAlpha) { | |
return keyPresses. | |
map(function (e) { return String.fromCharCode(e.keyCode); }). | |
// Ensure we only have alphabetic characters | |
filter(function (character) { return isAlpha(character); }). | |
// TODO: Filter out successive repetitive keys | |
distinctUntilChanged(). | |
// Building up a string of all the characters typed. | |
scan(function (stringSoFar, character) { | |
return stringSoFar + character; | |
}, ''); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 41: Autocomplete Box Part 2: Electric Boogaloo | |
function (getSearchResultSet, keyPresses, textBox) { | |
var getSearchResultSets = | |
keyPresses. | |
map(function () { | |
return textBox.value; | |
}). | |
throttleTime(1000). | |
// TODO: Make sure we only get distinct values | |
distinctUntilChanged(). | |
// TODO: Make sure the text is not empty | |
filter((v) => v.length > 0). | |
concatMap(function (text) { | |
return getSearchResultSet(text).takeUntil(keyPresses); | |
}); | |
return getSearchResultSets; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment