Why my solution doesnt work on Functional Programming - Use the reduce Method to Analyze Data

Tell us what’s happening:
why my solution doesnt work

let getRating = (watchList, directorName) => {

    let targetDirector = watchList.filter(x => x["Director"] ===  directorName);

  let num = targetDirector.map(k => Number(k["imdbRating"]))

  let total = num.reduce((currentTotal, item) => currentTotal + item)

  let totalAverageRating = total / targetDirector.length

  // Only change code above this line 

  return totalAverageRating;

}

console.log(getRating(watchList,'Christopher Nolan'))

even though the result is the same in the hint section

Your code so far

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/100.0.4896.60 Safari/537.36 Edg/100.0.1185.29

Challenge: Use the reduce Method to Analyze Data

Link to the challenge:

It’s not working because you’ve changed how it works.

The purpose of the function is to return the average rating for “Christopher Nolan” films.

You have enhanced the function to allow it to search for any director. This is great: it makes it much more useful!

But your function will fail the tests, because the tests will run:

getRating(watchList)

In your code, this line:

let targetDirector = watchList.filter(x => x["Director"] ===  directorName);

directorName wil be undefined. The result of that filter will therefore be []. There are no directors who are undefined.


To fix, either:

Remove the second parameter (directorName). Replace directorName in the filter with "Christopher Nolan":

let getRating = (watchList) => {
  let targetDirector = watchList.filter(x => x["Director"] ===  "Christopher Nolan");

In this case, hard-code the function to only search for Christopher Nolan films. Or…

Keep what you have. Pass a default value for the parameter:

let getRating = (watchList, directorName = "Christopher Nolan")  => {
  let targetDirector = watchList.filter(x => x["Director"] ===  directorName);

In this case, if directorName is undefined, then use the value "Christopher Nolan"

2 Likes

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