.reduce() challenge not meeting requirement

Tell us what’s happening:


I’m having trouble getting the correct average for a collection of movies that are made by a particular director. What I’m doing is the following…

  • Filter the watchList array to only contain movies made by a certain director
  • Map over each movie to get it’s imdbRating and coerce it into a Number
  • Reduce the mapped array to a single number that is summation of the filtered ratings
  • To get the average, I take the return value of reduce and divide it by length of the filtered array in Step 1.

The challenge is expecting a value of 8.675, but upon inspection I’m getting 8.25. I’m not sure what is going wrong and why I’m not getting 8.675?


function getRating(watchList){
  // Only change code below this line
  var nolan_movies = watchList.filter(function(movie) {
    return movie.Director === "Christopher Nolan";
  }).map(function(movie) {
    return parseInt(movie.imdbRating);
  });
  var averageRating = nolan_movies.reduce(function(acc, cur) {
    return acc + cur;
  }) / nolan_movies.length;

  // Only change code above this line
  return averageRating;
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0.

Challenge: Use the reduce Method to Analyze Data

Link to the challenge:

parseInt gives you a whole number, you can check by adding console.log(nolan_movies) in your function

1 Like

Adding to @ieahleen, parseInt() only returns integers(whole numbers). You can use the parseFloat() to get a return of decimals.