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
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 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.
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;
}
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