Array filter() and map() challenge

Hi all,
So I’m stuck again . It looks like my code returns 4 objects like it should , but the test is not passing.
Here’s my code:

var filteredRatings =[];
let data="";
let str="";
let d = "";

filteredRatings= watchList.filter(index =>{
	data = [{rating:index.imdbRating}];
	str = JSON.stringify(data[0].rating);
	d = JSON.parse(str);
	d = parseInt(d);
	return (d >=8);	
}).map((index) => {
    return ({title:index.Title ,rating:index.imdbRating });
});

The watchlist is a huge array that I’m not going to post here , but it should be there in the challenge. The idea is to use map() and filter() to first filter out array elements that don’t have a imbdRating of larger than 8 , and then map the remaining elements.

I would like to know why my code is not correct , before looking at other people’s code.
I hope someone can help.

Thanks :slight_smile:

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

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/functional-programming/use-the-filter-method-to-extract-data-from-an-array

filteredRatings should be named filteredList
(test 4)

I’m curious about why you’re converting to JSON and back. watchList is already an array of objects, so no conversion is necessary.

What I mean is, your filter could simply be

watchList.filter(movie => parseFloat(movie.imdbRating) >= 8);

Lastly, naming map and filter's first parameter index is confusing. The first parameter is the element, and the second is the index of that element in the array.

2 Likes

Whoa, and I just looked again and saw that you’re declaring variables in the global scope that get modified inside the callback functions.

var filteredRatings =[];
let data="";
let str="";
let d = "";

In functional programming you should never do this. Functions should create no side effects such as modifying variables outside their scope, they should only produce some output depending solely on their input.

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/functional-programming/refactor-global-variables-out-of-functions

1 Like

Yes, I see that now , I’m still new to JS and was focusing too much on the overal solution.
I’m not sure what you mean with that I’m converting to JSON and back? I found this solution at stackoverflow to convert an string object to a number? It does seem to correctly do that?
I will have to look at this much more, I know . Hoping meanwhile for some more insights. Thanks :slight_smile:

this is what I have now and it’s still not passing:

const filteredRatings= watchList.filter(function(item){
    if(parseFloat(item.imdbRating)>=8){ return item;}
}).map(function(item){
    return ({title:item.Title ,rating:item.imdbRating });
});

console.log(filteredRatings);

I’d hate to waste much more time on this, it might be something really silly that I’m doing wrong ?

edit: nevermind, I need to change a variable name I had changed , so it’s solved at last :slight_smile:

.filter(function(item){
   if(parseFloat(item.imdbRating)>=8){ return item;}
})

You’re really close on this one, the bug here is that the filter callback function should return a boolean, not the item. Return true if the item should stay, or false if it should be filtered out.

@michaelsndr already give the answer I guess.
Your first code is working right, just rename the variable name filteredRating to

filteredList

Otherwise doesn’t matter what your code is, you won’t passed the test.