Functional Programming - Use the filter Method to Extract Data from an Array

Hello,
Could you plz tell me why my code does not show the right output?
const finalArray =;

const watchListEdit = watchList.filter(user => user.imdbRating>=8.0);

let filteredList= watchListEdit.map(function(x){

return finalArray.push({“title”: x.Title}, {“rating”: x.imdbRating})

})
Even though I have added the “” for both title and rating, in the console they appear without quotations and I am guessing that is why my code does not pass.
Please explain
thanks
Sara

Your code so far

WARNING

The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.

You will need to take an additional step here so the code you wrote presents in an easy to read format.

Please copy/paste all the editor code showing in the challenge from where you just linked.

Replace these two sentences with your copied code.
Please leave the ``` line above and the ``` line below,
because they allow your code to properly format in the post.

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36

Challenge: Functional Programming - Use the filter Method to Extract Data from an Array

Link to the challenge:

Hi, Sara!

From reading the description of this task, I understand that only 2 methods should be used: filter & map. Method filter will filter out the items that would match provided criteria (rating higher or equal than 8.0 in this case) and method map would iterate over the filtered list and allow to return a new, modified list that would only contain of objects with title & rating properties.

The problem in your provided example is, I believe, in a way you’re trying to push new object to the specified array. You’re passing 2 arguments, two separate objects, to push method, instead of 1.

The way I see solution to this problem is pretty straightforward:

  1. Filter movies by rating
  2. Construct new array of movies with specified properties

I’d approach this like so:

const filteredList = watchList
     .filter(movie => movie.imdbRating >= 8.0)
     .map(movie => ({ title: movie.Title, rating: movie.imdbRating }))

Let me know if you have further questions or if something is still not clear to you.

Your code is good and nearly there. Below I’ve copied and pasted your code with some notes

//Be aware of the name of the variable the challenge wants you to return: 
// filteredList must equal the returned variable according to the challenge criteria
// not "finalArray"
const finalArray =[];

const watchListEdit = watchList.filter(user => user.imdbRating>=8.0);

let filteredList= watchListEdit.map(function(x){

return finalArray.push({“title”: x.Title}, {“rating”: x.imdbRating})
/*above ^ you're returning two objects one for title and one for rating, they should be together
Also, you don't need the quotation marks around title and rating*/
})

Hopefully this helps clear up any confusion. Keep up the good coding and well done using map and filter!

I think you might not be fully aware how map works. It goes throught every element in an array. The callback you pass to it describes what every element would go through and what the outcome would be.

arr.map(callback)

function callback (element, index) {
  return element + 1
}

In the code, the current element you execute the callback with, is the first parameter. You can add the optional second parameter to track the element index. What you return in the end of the callback, is what that elemeny will turn out as result. For example, i could assume the element is some number and i want to add 1 to that number(to each number in the array), so in the end, the array i map, will have all elements(numbers) up one point. Making any manipulations with the original array(like arr.push), would go against the purpose of map and other similar array methods. They are not meant to mutate the original array, only give us a new copy of it, making some changes on the way. We dont want to(in majority of cases) refer to the actualy array within the callback and make any changes to it, like push. If i wanted to mutate the original array(make the changes directly to it), id prolly initialize a new array variable, use a for loop and push to it modified elements of the original array, which would pretty much produce the same result as map the original without push(but with more compelx syntax). Ofc, the purpose of the parituclar challenge is to use filter and map, isntead of the more wordy for/push etc flow.

Thank you for your reply. But honestly I dont see any difference between my code and yours except that you have combined the two filter and map method.
The output of my code is completely right except that title and rating dont appear with quotations.

As I’ve explained:

The problem in your provided example is, I believe, in a way you’re trying to push new object to the specified array. You’re passing 2 arguments, two separate objects, to push method, instead of 1.

Hi,
Thanks for your reply. here is the output of my code in the console:
[ { title: ‘Inception’ },
{ rating: ‘8.8’ },
{ title: ‘Interstellar’ },
{ rating: ‘8.6’ },
{ title: ‘The Dark Knight’ },
{ rating: ‘9.0’ },
{ title: ‘Batman Begins’ },
{ rating: ‘8.3’ } ]
very close but with no quotation. I removed the quotations in my code as you said. no difference.

I am doing this exactly like the example provided by freecodecamp as below:
let titleRating = watchList.map(function(X){
return ratings.push({title: X.Title , rating: X.imdbRating }).

@Honey1835 ,

Is it really “exactly” as it’s shown? You’ve mentioned this a couple times now and I’ve even repeated the part of my answer where the problem is.

You’re passing two separate objects to push method, while it should only take 1, that being a movie with 2 properties. Instead, you’re passing 2 different objects, one with title, and another one with rating as their only properties.

Ohhhh I got it. Thank you so much. And sorry for not listening to your point.
Problem solved.

There are two objects you’re returning and one for title and one for rating, they should be returned together as one object.