Why does this solution work? Use the filter Method to Extract Data from an ArrayPassed

Tell us what’s happening:
I’m confused why this solution works. Don’t we need to convert the imdbRating from a string to an integar?

How does the current filter work if it’s checking for an integar (“8”) against a string (the imdbRating)?

Your code so far


var filteredList = watchList
.map(function(e){
  return {title: e["Title"], rating: e["imdbRating"]};
})
.filter(e => e.rating >= 8);

Your browser information:

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

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

Link to the challenge:

JavaScript isn’t a strongly typed language, it will try to coerce values before evaluation rather than throwing a TypeError or something like that. In this case, it converts the e.rating string to a number for you, and then compares it against 8.

Sometimes this type coercion is unpredictable, so it’s generally best practice to do it explicitly. In this case you could use Number.parseFloat in the filter function.

1 Like
1 Like