# Use the reduce Method to Analyze Data - Appending decimals

I am trying to solve this problem a piece at a time with the following:
(Credit to pseudospencer for his approach to the problem. )

// create a list of Christopher Nolan movies
let CNmovies = watchList.filter(movie => movie.Director == “Christopher Nolan”);

// reduce to the sum of all of his movie ratings
let allRatingsSummed = CNmovies.reduce((acc, movie) => acc + parseFloat(movie.imdbRating));

But this is resulting in allRatingsSummed looking like this in the console:
object Object]8.698.3

I’m getting the imdbRatings appended to each other rather than summed. (???) And the first movie object seems to be missing altogether. But if I could just get the ratings to be treated as numbers, I would be happy for now. Note: I have tried both using the parseFloat() and Number() functions to convert the rating to a number.

Does anyone see what I’m doing wrong?
Thanks!

You are misunderstanding two things about the reduce method:

#1) If you do not specify an initialize value (see documentation link above) for the accumulator argument in the callback function, then during the first iteration of the array, the accumulator (the 1st argument) will be the first item and the second argument will be the 2nd element in the array. This is why there would be one less iteration than you expect.

#2) You will notice that movie.imdbRating is a string value. Whenever you use the + operator between two values and one of those values is a string, then both values are treated as strings and get concatenated together. For example:

``````var myStr ="Good Times";
var num = 15;
var merge = num + myStr; // merge is now "15Good Times"
``````

You need to convert any string values to numbers before trying to using the + operator.

I suggest you read about the 2nd (optional) parameter of the filter method which represents the initial value of the accumulator. 2nd parameter should not to be confused with the 2nd argument of the callback function. These are two completely different values.

1 Like

Thank you!! That was really twisting my noodle. All I had to do was give the initialize value. The parseFloat was converting the string just fine. It was an object being in the first argument that was messing up my math.