1. Use a For loop to traverse the videos and bookmarks array at the same time. For each video and bookmark pair, create a {videoid, bookmarkid} pair and add it to the Videoidandbookmarkidpairs array.
function () {varVideos = [ { "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++) { Videoidandbookmarkidpairs.push ({videoid:videos[counter].id, bookmarkid:bookmarks[c Ounter].id}) }returnVideoidandbookmarkidpairs;}
2. Let's add a static zip () function to the Array type. The ZIP function accepts a combiner function, traverses each array at the same time, and calls the combiner function on th E current item on the Left-hand-side and Right-hand-side. The ZIP function requires an item from each of the array in order to call the Combiner function, therefore the array returned by Zip'll only is as large as the smallest input array.
//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) {varcounter, 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 arraysResults.push (Combinerfunction (Left[counter], Right[counter])}returnresults;};
3. Let's repeat Exercise 1, but this time lets use your new zip () function. For each video and bookmark pair, create a {videoid, bookmarkid} pair.
function () {varVideos = [ { "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} ]; returnArray. zip (videos, bookmarks, (Video,bookmark) = ({videoId:video.id, bookmarkId:bookmark.id} )}
[Javascript] Implement zip function