Functional Programming - Use the reduce Method to Analyze Data

Tell us what’s happening:
Hmm okay this is fairly straight forward, after every exercise I submit then go back to the same page, open hints, results, copy them in vsCode and press forward again. ( mostly because what I have experienced with codewars is that seeing different results goes a long way in learning ). Sorry, back to the point, in this particular exercise your solutions will return NaN so you would have to update them to Number(averageRating) before returning. I believe this to be because average.Rating is returning a string in both solutions when the answer expected is a pure integer.
Your code so far
Sowwy no code :'3
WARNING

The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.

You will need to take an additional step here so the code you wrote presents in an easy to read format.

Please copy/paste all the editor code showing in the challenge from where you just linked.

Replace these two sentences with your copied code.
Please leave the ``` line above and the ``` line below,
because they allow your code to properly format in the post.

Your browser information:

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

Challenge: Functional Programming - Use the reduce Method to Analyze Data

Link to the challenge:

We really need to see your code to comment on how to fix your code.

Its not my code that needs fixing its your solutions, go to this same exercise, pass in your solutions and you will see that they don’t work. Because your output isn’t an integer, its most likely a string so you have to Number() it.

I just verified that the solutions work fine.

Maybe you can post your code so we can help you fix the problem in it?

Both the possible solutions in the hints explicitly convert the string to a number though?

Also is it not more useful to save your solution rather than the example solutions, which are just possible ways to solve the challenges

1 Like

I already finished the exercise, then redone it with your solutions which I couldn’t complete the exercise with, because in both cases I had to take the result of averageRating/watchlist.filter and Number() that result, if solutions work fine for you then I am glad, I literally copy pasted them and to me they do not work unless you Number(solution) before returning them. But as I said if they work for other people then good I have no problem, I’m not stuck at that exercise either so theres nothing else to add.

Why can’t you post the code you used? It’s literally impossible for us to replicate without the exact same code you used.

This ensures there is a number:

film => Number(film.imdbRating)

and this

data.sum += Number(rating);

Both of the solutions convert the string to a number, the result is always a number

edit:

function getRating(watchList){
  // Add your code below this line
  const averageRating = watchList
    // Use filter to find films directed by Christopher Nolan
    .filter(film => film.Director === "Christopher Nolan")
    // !------- HERE:
    // Use map to convert their ratings from strings to numbers
    .map(film => Number(film.imdbRating))
    // !------- 
    // Use reduce to add together their ratings
    .reduce((sumOfRatings, rating) => sumOfRatings + rating) /
  // Divide by the number of Nolan films to get the average rating
  watchList.filter(film => film.Director === "Christopher Nolan").length;
  // Add your code above this line
  return averageRating;
}

and

function getRating(watchList) {
  // Add your code below this line
  const nolanData = watchList
    .reduce((data, { Director: director, imdbRating: rating }) => {
      if (director === 'Christopher Nolan') {
        data.count++;
        // !------- HERE:
        data.sum += Number(rating);
        // !-------
      }
      return data;
    }, { sum: 0, count: 0 });
  const averageRating = nolanData.sum / nolanData.count;
  // Add your code above this line
  return averageRating;
}
1 Like

xd I juse, copy, pasted your solutions

const averageRating = watchList
   
 averageRating .filter(film => film.Director === "Christopher Nolan").map(film => Number(film.imdbRating)) .reduce((sumOfRatings, rating) => sumOfRatings + rating) / watchList.filter(film => film.Director === "Christopher Nolan").length;

or the same but

const averageRating = watchList
  
averageRating .filter(film => film.Director === "Christopher Nolan").map(film => Number(film.imdbRating)) .reduce((sumOfRatings, rating) => sumOfRatings + rating) 
averageRating / watchList.filter(film => film.Director === "Christopher Nolan").length;

or

const averageRating = watchList
   
 averageRating .filter(film => film.Director === "Christopher Nolan").map(film => Number(film.imdbRating)) .reduce((sumOfRatings, rating) => sumOfRatings + rating) 
let result = averageRating / watchList.filter(film => film.Director === "Christopher Nolan").length;

  return result; // In which case yes I do already know result returns a string that needs to be converted

or

const nolanData = watchList
    .reduce((data, { Director: director, imdbRating: rating }) => {
      if (director === 'Christopher Nolan') {
        data.count++;
        data.sum += Number(rating);
      }
      return data;
    }, { sum: 0, count: 0 });
  const averageRating = nolanData.sum / nolanData.count;

  return averageRating;

This is not one of our solutions. It is missing the explicit cast to a number in the map.

This is also not one of our solutions and it also is missing the explicit cast

This has the explicit cast and should be fine.

I don’t know where the first three came from, but they are not solutions we’ve posted anywhere?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.