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
Challenge: Use the filter Method to Extract Data from an Array
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.
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.
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
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.