1

I have been working through a study guide on Github and I am trying to work through a problem that chains filter and map to get the answer. So, I have an array and I'm supposed to use the two methods to select the ids of the videos with a rating of 5.0. Here's the array:

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 }]
    }
];

Now My code is as follows:

  newReleases.filter(function(video){
    return video.rating == 5.0;
  }).map (function(video) {
    return { id: video.id } 
  });
}

My thought process is I first call the filter method and apply a test (video.rating == 5.0) and return the elements that pass the test. Then I use the map method to apply a function to the elements that passed the test to return what I want, the ids of the videos that have a 5.0 rating, by having map return 'video.id'. Now when I execute, I get this error:

TypeError: 'undefined' is not an object (evaluating 'videoids.sortBy')

This error code is really throwing me off, because I don't see a 'videoids' variable in the array and it doesn't appear that I need to apply a .SortBy.

So I am asking for clarification on the error code please. I have also tried this code in a number of different fashions and have not come up with the correct one, so I would really appreciate a walk through of what I am not doing. The reason I feel confident in my code is because I have applied a very similar code to a very similar exercise in [Eloquent Javascript] at the end of Chapter 5 (Exercise 5.2 Mother-child age difference). I have spent hours (more than I'd like to admit) stuck on this and I have browsed stack overflow for about two hours. I could easily press the get answer button but I am afraid that I won't learn anything by doing that.

Could someone please assist me?

4
  • The code as you wrote it runs fine - jsfiddle.net/yvy06sn1 There must be something else in your code. Commented Feb 7, 2016 at 21:58
  • works here too on Chrome, what OS, browser version are you on? Restart your computer then retry! Commented Feb 7, 2016 at 21:59
  • i am running the code through the study guide. I am running it on safari ver. 6.1.3 (not my computer). I tried to restart didn't work unfortunately. Thanks for responding though! Commented Feb 7, 2016 at 22:28
  • I solved it. What was happening wasn't anything to do with the code. It was two things. First the browser I was running on was outdated and it could have attributed. Second, it was the orientation of my return from .map. I was supposed to put 'return video.id' rather than 'return { id: video.id}' Thanks everyone for your comments, sometimes it helps to just talk it out. Commented Feb 7, 2016 at 23:01

2 Answers 2

2

Please add return. Otherwise you are undefined returning.

return newReleases.filter(function(video) {
    //...
Sign up to request clarification or add additional context in comments.

2 Comments

I added the return and I am no longer getting the undefined. The browser still finds something wrong with my answer though. Hmmm, maybe i should try running it on an empty browser outside the study guide.
Just to be clear, nothing is wrong with my code other that the return?
0

You must be doing something wrong because this works me. See https://jsfiddle.net/887mfh10/1/.

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 }]
    }
];

var ids = newReleases.filter(function(video){
    return video.rating == 5.0;
  }).map (function(video) {
    return { id: video.id } 
  });

alert(JSON.stringify(ids)); // see console

2 Comments

Thanks Amir, that seems to be the consensus. I think it may have something to do with my browser. Can I get a clarification on the error code I received?
I think I just answered my own question. Since I didn't return the filter or declare a variable in front of it, it was returning undefined, right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.