I am wondering how .reduce((sumRate,rate) => sumRate + rate) knows to assign the sum of imdbRatings to sumRate. I don’t see where it links. Please help, Thank you!!!
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/102.0.5005.115 Safari/537.36
I am wondering how .reduce((sumRate,rate) => sumRate + rate) knows to assign the sum of imdbRatings to sumRate. I don’t see where it links. Please help, Thank you!!!
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.
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")
// 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;
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.115 Safari/537.36
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
I assume you know how map and filter work since they are covered in lessons before this. If you check the documentation it gives a much more detailed explanation of how reduce works.
I am sorry I don’t have the answer to your question I just wanted to post my unique answer here since I can’t find a way to do that in free code camp
Anyway, here is it.
function getRating(watchList) {
// Only change code below this line
let averageRating = watchList
// gets the movies produced by Christopher Nolan
.filter(movie => movie.Director === "Christopher Nolan")
//gets the average with the callback function having all
// four arguments and the
// index must be included because without it will return NaN.
// I don't know the
// reason though I will be happy to know why
.reduce((avg, movie, index, array) => {
avg + parseFloat(movie.imdbRating) / array.length, 0 )
}
// In the first iteration the avg = 0 + movie.imdbRating
// of the first movie / array.length = 5
// the result of the first iteration becomes the value of
// the avg in the second
//interation and so on... till the last iteration where we get
// the avg(average).
// for example the 5 values (1, 2, 3, 4, 5)
// will be 0 +1/5 => 1/5 + 2/5 => 3/5 + 3/5 =>
// 6/5 + 4/5 => 2 + 4/5 = 3
// Only change code above this line
return averageRating;
}