freeCodeCamp - Use the reduce Method to Analyze Data - Question

Hello, i managed to do it but with some troubles :confused:
Link: https://beta.freecodecamp.org/en/challenges/functional-programming/use-the-reduce-method-to-analyze-data

My question is, why do i have an undefined value in my filteredList variable near the beginning ?
I’m not feeling good with map() reduce() and filter() functions mixed up with objects. It’s pretty hard.

// the global variable
var watchList = [
                 {  
                   "Title": "Inception",
                   "Year": "2010",
                   "Rated": "PG-13",
                   "Released": "16 Jul 2010",
                   "Runtime": "148 min",
                   "Genre": "Action, Adventure, Crime",
                   "Director": "Christopher Nolan",
                   "Writer": "Christopher Nolan",
                   "Actors": "Leonardo DiCaprio, Joseph Gordon-Levitt, Ellen Page, Tom Hardy",
                   "Plot": "A thief, who steals corporate secrets through use of dream-sharing technology, is given the inverse task of planting an idea into the mind of a CEO.",
                   "Language": "English, Japanese, French",
                   "Country": "USA, UK",
                   "Awards": "Won 4 Oscars. Another 143 wins & 198 nominations.",
                   "Poster": "http://ia.media-imdb.com/images/M/MV5BMjAxMzY3NjcxNF5BMl5BanBnXkFtZTcwNTI5OTM0Mw@@._V1_SX300.jpg",
                   "Metascore": "74",
                   "imdbRating": "8.8",
                   "imdbVotes": "1,446,708",
                   "imdbID": "tt1375666",
                   "Type": "movie",
                   "Response": "True"
                },
                {  
                   "Title": "Interstellar",
                   "Year": "2014",
                   "Rated": "PG-13",
                   "Released": "07 Nov 2014",
                   "Runtime": "169 min",
                   "Genre": "Adventure, Drama, Sci-Fi",
                   "Director": "Christopher Nolan",
                   "Writer": "Jonathan Nolan, Christopher Nolan",
                   "Actors": "Ellen Burstyn, Matthew McConaughey, Mackenzie Foy, John Lithgow",
                   "Plot": "A team of explorers travel through a wormhole in space in an attempt to ensure humanity's survival.",
                   "Language": "English",
                   "Country": "USA, UK",
                   "Awards": "Won 1 Oscar. Another 39 wins & 132 nominations.",
                   "Poster": "http://ia.media-imdb.com/images/M/MV5BMjIxNTU4MzY4MF5BMl5BanBnXkFtZTgwMzM4ODI3MjE@._V1_SX300.jpg",
                   "Metascore": "74",
                   "imdbRating": "8.6",
                   "imdbVotes": "910,366",
                   "imdbID": "tt0816692",
                   "Type": "movie",
                   "Response": "True"
                },
                {
                   "Title": "The Dark Knight",
                   "Year": "2008",
                   "Rated": "PG-13",
                   "Released": "18 Jul 2008",
                   "Runtime": "152 min",
                   "Genre": "Action, Adventure, Crime",
                   "Director": "Christopher Nolan",
                   "Writer": "Jonathan Nolan (screenplay), Christopher Nolan (screenplay), Christopher Nolan (story), David S. Goyer (story), Bob Kane (characters)",
                   "Actors": "Christian Bale, Heath Ledger, Aaron Eckhart, Michael Caine",
                   "Plot": "When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, the caped crusader must come to terms with one of the greatest psychological tests of his ability to fight injustice.",
                   "Language": "English, Mandarin",
                   "Country": "USA, UK",
                   "Awards": "Won 2 Oscars. Another 146 wins & 142 nominations.",
                   "Poster": "http://ia.media-imdb.com/images/M/MV5BMTMxNTMwODM0NF5BMl5BanBnXkFtZTcwODAyMTk2Mw@@._V1_SX300.jpg",
                   "Metascore": "82",
                   "imdbRating": "9.0",
                   "imdbVotes": "1,652,832",
                   "imdbID": "tt0468569",
                   "Type": "movie",
                   "Response": "True"
                },
                {  
                   "Title": "Batman Begins",
                   "Year": "2005",
                   "Rated": "PG-13",
                   "Released": "15 Jun 2005",
                   "Runtime": "140 min",
                   "Genre": "Action, Adventure",
                   "Director": "Christopher Nolan",
                   "Writer": "Bob Kane (characters), David S. Goyer (story), Christopher Nolan (screenplay), David S. Goyer (screenplay)",
                   "Actors": "Christian Bale, Michael Caine, Liam Neeson, Katie Holmes",
                   "Plot": "After training with his mentor, Batman begins his fight to free crime-ridden Gotham City from the corruption that Scarecrow and the League of Shadows have cast upon it.",
                   "Language": "English, Urdu, Mandarin",
                   "Country": "USA, UK",
                   "Awards": "Nominated for 1 Oscar. Another 15 wins & 66 nominations.",
                   "Poster": "http://ia.media-imdb.com/images/M/MV5BNTM3OTc0MzM2OV5BMl5BanBnXkFtZTYwNzUwMTI3._V1_SX300.jpg",
                   "Metascore": "70",
                   "imdbRating": "8.3",
                   "imdbVotes": "972,584",
                   "imdbID": "tt0372784",
                   "Type": "movie",
                   "Response": "True"
                },
                {
                   "Title": "Avatar",
                   "Year": "2009",
                   "Rated": "PG-13",
                   "Released": "18 Dec 2009",
                   "Runtime": "162 min",
                   "Genre": "Action, Adventure, Fantasy",
                   "Director": "James Cameron",
                   "Writer": "James Cameron",
                   "Actors": "Sam Worthington, Zoe Saldana, Sigourney Weaver, Stephen Lang",
                   "Plot": "A paraplegic marine dispatched to the moon Pandora on a unique mission becomes torn between following his orders and protecting the world he feels is his home.",
                   "Language": "English, Spanish",
                   "Country": "USA, UK",
                   "Awards": "Won 3 Oscars. Another 80 wins & 121 nominations.",
                   "Poster": "http://ia.media-imdb.com/images/M/MV5BMTYwOTEwNjAzMl5BMl5BanBnXkFtZTcwODc5MTUwMw@@._V1_SX300.jpg",
                   "Metascore": "83",
                   "imdbRating": "7.9",
                   "imdbVotes": "876,575",
                   "imdbID": "tt0499549",
                   "Type": "movie",
                   "Response": "True"
                }
];

function findAvgMovies( watchList, director ){
    let filteredList = watchList.map( function( obj ){
		if( obj.Director == director ) {
			let rating = obj.imdbRating;
			
			return rating;
		}
    })
	
	//["8.8", "8.6", "9.0", "8.3", undefined]
	filteredList.splice( filteredList.length - 1 );
	
	let valueAvg = filteredList.reduce( function( a, b ) {
		return ( parseFloat( a ) + parseFloat( b ) );
	}) / filteredList.length;
	
	return valueAvg;
};

var averageRating = findAvgMovies( watchList, "Christopher Nolan" ) ;
console.log( averageRating );

If I’m not mistaken it’s because you are using map()—the director of the last movie in the array is James Cameron and not Christopher Nolan, so the last item is undefined because map() always give you an array of the same length as the input.

Give filter() a go (EDIT: the code will be much simpler with it and it’s designed for cases like this). :slight_smile:

2 Likes

Hello, here my new code:

/*
Link: https://beta.freecodecamp.org/en/challenges/functional-programming/use-the-reduce-method-to-analyze-data
*/

// the global variable
var watchList = [
                 {  
                   "Title": "Inception",
                   "Year": "2010",
                   "Rated": "PG-13",
                   "Released": "16 Jul 2010",
                   "Runtime": "148 min",
                   "Genre": "Action, Adventure, Crime",
                   "Director": "Christopher Nolan",
                   "Writer": "Christopher Nolan",
                   "Actors": "Leonardo DiCaprio, Joseph Gordon-Levitt, Ellen Page, Tom Hardy",
                   "Plot": "A thief, who steals corporate secrets through use of dream-sharing technology, is given the inverse task of planting an idea into the mind of a CEO.",
                   "Language": "English, Japanese, French",
                   "Country": "USA, UK",
                   "Awards": "Won 4 Oscars. Another 143 wins & 198 nominations.",
                   "Poster": "http://ia.media-imdb.com/images/M/MV5BMjAxMzY3NjcxNF5BMl5BanBnXkFtZTcwNTI5OTM0Mw@@._V1_SX300.jpg",
                   "Metascore": "74",
                   "imdbRating": "8.8",
                   "imdbVotes": "1,446,708",
                   "imdbID": "tt1375666",
                   "Type": "movie",
                   "Response": "True"
                },
                {  
                   "Title": "Interstellar",
                   "Year": "2014",
                   "Rated": "PG-13",
                   "Released": "07 Nov 2014",
                   "Runtime": "169 min",
                   "Genre": "Adventure, Drama, Sci-Fi",
                   "Director": "Christopher Nolan",
                   "Writer": "Jonathan Nolan, Christopher Nolan",
                   "Actors": "Ellen Burstyn, Matthew McConaughey, Mackenzie Foy, John Lithgow",
                   "Plot": "A team of explorers travel through a wormhole in space in an attempt to ensure humanity's survival.",
                   "Language": "English",
                   "Country": "USA, UK",
                   "Awards": "Won 1 Oscar. Another 39 wins & 132 nominations.",
                   "Poster": "http://ia.media-imdb.com/images/M/MV5BMjIxNTU4MzY4MF5BMl5BanBnXkFtZTgwMzM4ODI3MjE@._V1_SX300.jpg",
                   "Metascore": "74",
                   "imdbRating": "8.6",
                   "imdbVotes": "910,366",
                   "imdbID": "tt0816692",
                   "Type": "movie",
                   "Response": "True"
                },
                {
                   "Title": "The Dark Knight",
                   "Year": "2008",
                   "Rated": "PG-13",
                   "Released": "18 Jul 2008",
                   "Runtime": "152 min",
                   "Genre": "Action, Adventure, Crime",
                   "Director": "Christopher Nolan",
                   "Writer": "Jonathan Nolan (screenplay), Christopher Nolan (screenplay), Christopher Nolan (story), David S. Goyer (story), Bob Kane (characters)",
                   "Actors": "Christian Bale, Heath Ledger, Aaron Eckhart, Michael Caine",
                   "Plot": "When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, the caped crusader must come to terms with one of the greatest psychological tests of his ability to fight injustice.",
                   "Language": "English, Mandarin",
                   "Country": "USA, UK",
                   "Awards": "Won 2 Oscars. Another 146 wins & 142 nominations.",
                   "Poster": "http://ia.media-imdb.com/images/M/MV5BMTMxNTMwODM0NF5BMl5BanBnXkFtZTcwODAyMTk2Mw@@._V1_SX300.jpg",
                   "Metascore": "82",
                   "imdbRating": "9.0",
                   "imdbVotes": "1,652,832",
                   "imdbID": "tt0468569",
                   "Type": "movie",
                   "Response": "True"
                },
                {  
                   "Title": "Batman Begins",
                   "Year": "2005",
                   "Rated": "PG-13",
                   "Released": "15 Jun 2005",
                   "Runtime": "140 min",
                   "Genre": "Action, Adventure",
                   "Director": "Christopher Nolan",
                   "Writer": "Bob Kane (characters), David S. Goyer (story), Christopher Nolan (screenplay), David S. Goyer (screenplay)",
                   "Actors": "Christian Bale, Michael Caine, Liam Neeson, Katie Holmes",
                   "Plot": "After training with his mentor, Batman begins his fight to free crime-ridden Gotham City from the corruption that Scarecrow and the League of Shadows have cast upon it.",
                   "Language": "English, Urdu, Mandarin",
                   "Country": "USA, UK",
                   "Awards": "Nominated for 1 Oscar. Another 15 wins & 66 nominations.",
                   "Poster": "http://ia.media-imdb.com/images/M/MV5BNTM3OTc0MzM2OV5BMl5BanBnXkFtZTYwNzUwMTI3._V1_SX300.jpg",
                   "Metascore": "70",
                   "imdbRating": "8.3",
                   "imdbVotes": "972,584",
                   "imdbID": "tt0372784",
                   "Type": "movie",
                   "Response": "True"
                },
                {
                   "Title": "Avatar",
                   "Year": "2009",
                   "Rated": "PG-13",
                   "Released": "18 Dec 2009",
                   "Runtime": "162 min",
                   "Genre": "Action, Adventure, Fantasy",
                   "Director": "James Cameron",
                   "Writer": "James Cameron",
                   "Actors": "Sam Worthington, Zoe Saldana, Sigourney Weaver, Stephen Lang",
                   "Plot": "A paraplegic marine dispatched to the moon Pandora on a unique mission becomes torn between following his orders and protecting the world he feels is his home.",
                   "Language": "English, Spanish",
                   "Country": "USA, UK",
                   "Awards": "Won 3 Oscars. Another 80 wins & 121 nominations.",
                   "Poster": "http://ia.media-imdb.com/images/M/MV5BMTYwOTEwNjAzMl5BMl5BanBnXkFtZTcwODc5MTUwMw@@._V1_SX300.jpg",
                   "Metascore": "83",
                   "imdbRating": "7.9",
                   "imdbVotes": "876,575",
                   "imdbID": "tt0499549",
                   "Type": "movie",
                   "Response": "True"
                }
];

function myDirectors( obj ) {
	if( obj.Director == "Christopher Nolan" ) {
		return true;
	}
};

function findAvgMovies( watchList, director ){
	let directorList = watchList.filter( myDirectors );
	
	//["8.8", "8.6", "9.0", "8.3"]
    let ratingList = directorList.map( function( obj ){
		if( obj.Director == director ) {
			let rating = obj.imdbRating;
			
			return rating;
		}
    });
	
	let valueAvg = ratingList.reduce( function( a, b ) {
		return ( parseFloat( a ) + parseFloat( b ) );
	}) / ratingList.length;
	
	return valueAvg;
};

var averageRating = findAvgMovies( watchList, "Christopher Nolan" ) ;
console.log( averageRating );

But i need help with myDirectors function. I can’t make the variable “director” global so i can use it in my “myDirectors()”

Thanks you. It’s working.
But i’m not sure to understand clearly the “return function(obj) {…};” code. You got back the director variable in the parameter. But now the obj is missing. But you managed to have it back like this… It’s a way to grabe back the “watchList” variable object. But like this… if you could explain a bit.

For the “only reduce()” stuff, i will continue to try out… i already “lost” like 20mn and managed to do nothing with it.
I’m using this from freeCodeCamp https://medium.freecodecamp.org/reduce-f47a7da511a9
if you do have some very usefull stuff to help me to figure it out how to use .reduce() on object and undestand how it performe. Some thing i have trouble to understand is the PRECISE role of what variables in reduce parameter do.
For instance in a simple forloop, i know each value the variables contains but i know how it work. But not really, or at all, for the variable in these types of fonctions like reduce(), map() …

edit: and last, who are you ? you helped me every time i asked :slight_smile:

Ok thanks you very much for the time spend helping me. I will give a look at it tomorrow :slight_smile:
Have a good day/night.

Hello, i will try to see the documentation but i still don’t understand the role of the “index”:

var euros = [29.76, 41.85, 46.5]; 

var sum = euros.reduce( function( value, index, euros ){
	return value + index;
	// return value + index;
});

console.log( sum ); //118.11
/*
value:  8   10   9   7   6
index:  0    1   2   4   5
euros:[29.76, 41.85, 46.5]
*/

Because it’s like:
total += 8+0;
total += 10+1
total += 9+2;
total += 7+4;
total += 6+5;

But i’m sure it’s false and it doesn’t make sens.

Try this

var averageRating = watchList.filter(elem => elem["Director"] === "Christopher Nolan").reduce((total,elem,index,arr)=>{
  return total+Number(elem["imdbRating"])/arr.length;
},0);
5 Likes

I ran your code successfully,
however I am confused why it works…


var averageRating = 
  watchList
    .filter(elem => elem.Director === "Christopher Nolan")
    .reduce((total,elem,index,arr) => {
      //console.log(total, elem.imdbRating, total + Number(elem.imdbRating) /arr.length);
      return total + Number(elem.imdbRating) /arr.length;
},0);


The console.log statement above outputs:

> 0 "8.8" 2.2 
> 2.2 "8.6" 4.35 
> 4.35 "9.0" 6.6  
> 6.6 "8.3" 8.675  //8.675 is the correct answer

However, i assumed the output would look like this

> 0 "8.8" 2.2 // (0 + 8.8)/4 = 2.2
> 2.2 "8.6" 2.7  // (2.2 + 8.6)/4 = 2.7
> 2.7 "9.0" 2.925 // (2.7 + 9.0)/4 = 2.925
> 2.925 "8.3"  2.80625  // (2.925 + 8.3)/4 = 2.80625

What appears to be happening is that the previous divisions are disregarded in the accumulator:

> 0 "8.8" 2.2 //(8.8)/4 = 2.2
> 2.2 "8.6" 4.35 //(8.8+8.6)/4 = 4.35
> 4.35 "9.0" 6.6  //(8.8+8.6+9.0)/4 = 6.6
> 6.6 "8.3" 8.675 //(8.8+8.6+9.0+8.3)/4 = 8.675

Why is this happening?

3 Likes

In this line division is first performed. Then the result of division increase accumulator (total).

If you change the order of operations like this (making addition first) :

console.log(total, elem.imdbRating, (total + Number(elem.imdbRating)) /arr.length);
return (total + Number(elem.imdbRating)) / arr.length

You got your expected output:

3 Likes

Well, I tried to solve this problem just using reduce() - as randelldawson suggested. Here is my solution. What do you think of it? Could have been anyway better? Thank you!

// the global variable
var watchList = [
                 {  
                   "Title": "Inception",
                   "Year": "2010",
                   "Rated": "PG-13",
                   "Released": "16 Jul 2010",
                   "Runtime": "148 min",
                   "Genre": "Action, Adventure, Crime",
                   "Director": "Christopher Nolan",
                   "Writer": "Christopher Nolan",
                   "Actors": "Leonardo DiCaprio, Joseph Gordon-Levitt, Ellen Page, Tom Hardy",
                   "Plot": "A thief, who steals corporate secrets through use of dream-sharing technology, is given the inverse task of planting an idea into the mind of a CEO.",
                   "Language": "English, Japanese, French",
                   "Country": "USA, UK",
                   "Awards": "Won 4 Oscars. Another 143 wins & 198 nominations.",
                   "Poster": "http://ia.media-imdb.com/images/M/MV5BMjAxMzY3NjcxNF5BMl5BanBnXkFtZTcwNTI5OTM0Mw@@._V1_SX300.jpg",
                   "Metascore": "74",
                   "imdbRating": "8.8",
                   "imdbVotes": "1,446,708",
                   "imdbID": "tt1375666",
                   "Type": "movie",
                   "Response": "True"
                },
                {  
                   "Title": "Interstellar",
                   "Year": "2014",
                   "Rated": "PG-13",
                   "Released": "07 Nov 2014",
                   "Runtime": "169 min",
                   "Genre": "Adventure, Drama, Sci-Fi",
                   "Director": "Christopher Nolan",
                   "Writer": "Jonathan Nolan, Christopher Nolan",
                   "Actors": "Ellen Burstyn, Matthew McConaughey, Mackenzie Foy, John Lithgow",
                   "Plot": "A team of explorers travel through a wormhole in space in an attempt to ensure humanity's survival.",
                   "Language": "English",
                   "Country": "USA, UK",
                   "Awards": "Won 1 Oscar. Another 39 wins & 132 nominations.",
                   "Poster": "http://ia.media-imdb.com/images/M/MV5BMjIxNTU4MzY4MF5BMl5BanBnXkFtZTgwMzM4ODI3MjE@._V1_SX300.jpg",
                   "Metascore": "74",
                   "imdbRating": "8.6",
                   "imdbVotes": "910,366",
                   "imdbID": "tt0816692",
                   "Type": "movie",
                   "Response": "True"
                },
                {
                   "Title": "The Dark Knight",
                   "Year": "2008",
                   "Rated": "PG-13",
                   "Released": "18 Jul 2008",
                   "Runtime": "152 min",
                   "Genre": "Action, Adventure, Crime",
                   "Director": "Christopher Nolan",
                   "Writer": "Jonathan Nolan (screenplay), Christopher Nolan (screenplay), Christopher Nolan (story), David S. Goyer (story), Bob Kane (characters)",
                   "Actors": "Christian Bale, Heath Ledger, Aaron Eckhart, Michael Caine",
                   "Plot": "When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, the caped crusader must come to terms with one of the greatest psychological tests of his ability to fight injustice.",
                   "Language": "English, Mandarin",
                   "Country": "USA, UK",
                   "Awards": "Won 2 Oscars. Another 146 wins & 142 nominations.",
                   "Poster": "http://ia.media-imdb.com/images/M/MV5BMTMxNTMwODM0NF5BMl5BanBnXkFtZTcwODAyMTk2Mw@@._V1_SX300.jpg",
                   "Metascore": "82",
                   "imdbRating": "9.0",
                   "imdbVotes": "1,652,832",
                   "imdbID": "tt0468569",
                   "Type": "movie",
                   "Response": "True"
                },
                {  
                   "Title": "Batman Begins",
                   "Year": "2005",
                   "Rated": "PG-13",
                   "Released": "15 Jun 2005",
                   "Runtime": "140 min",
                   "Genre": "Action, Adventure",
                   "Director": "Christopher Nolan",
                   "Writer": "Bob Kane (characters), David S. Goyer (story), Christopher Nolan (screenplay), David S. Goyer (screenplay)",
                   "Actors": "Christian Bale, Michael Caine, Liam Neeson, Katie Holmes",
                   "Plot": "After training with his mentor, Batman begins his fight to free crime-ridden Gotham City from the corruption that Scarecrow and the League of Shadows have cast upon it.",
                   "Language": "English, Urdu, Mandarin",
                   "Country": "USA, UK",
                   "Awards": "Nominated for 1 Oscar. Another 15 wins & 66 nominations.",
                   "Poster": "http://ia.media-imdb.com/images/M/MV5BNTM3OTc0MzM2OV5BMl5BanBnXkFtZTYwNzUwMTI3._V1_SX300.jpg",
                   "Metascore": "70",
                   "imdbRating": "8.3",
                   "imdbVotes": "972,584",
                   "imdbID": "tt0372784",
                   "Type": "movie",
                   "Response": "True"
                },
                {
                   "Title": "Avatar",
                   "Year": "2009",
                   "Rated": "PG-13",
                   "Released": "18 Dec 2009",
                   "Runtime": "162 min",
                   "Genre": "Action, Adventure, Fantasy",
                   "Director": "James Cameron",
                   "Writer": "James Cameron",
                   "Actors": "Sam Worthington, Zoe Saldana, Sigourney Weaver, Stephen Lang",
                   "Plot": "A paraplegic marine dispatched to the moon Pandora on a unique mission becomes torn between following his orders and protecting the world he feels is his home.",
                   "Language": "English, Spanish",
                   "Country": "USA, UK",
                   "Awards": "Won 3 Oscars. Another 80 wins & 121 nominations.",
                   "Poster": "http://ia.media-imdb.com/images/M/MV5BMTYwOTEwNjAzMl5BMl5BanBnXkFtZTcwODc5MTUwMw@@._V1_SX300.jpg",
                   "Metascore": "83",
                   "imdbRating": "7.9",
                   "imdbVotes": "876,575",
                   "imdbID": "tt0499549",
                   "Type": "movie",
                   "Response": "True"
                }
];

// Add your code below this line

let averageRating;

function average(director) {
  let count = 0;
  return (sum, film, index, arr) => {
    if (film.Director === director) {
      count ++;
      return sum + Number(film.imdbRating);
    }
    if (index === arr.length -1) {
      return sum / count;
    }
    return sum;
  }
}

averageRating = watchList.reduce(average("Christopher Nolan"), 0);
// Add your code above this line

console.log(averageRating); 

My solution looks like this:

var count = 0;
var averageRating = watchList.reduce((acc, curr) => {
if (curr[“Director”] === “Christopher Nolan”) {
acc += Number(curr[“imdbRating”]);
count++;
}
return acc;

}, 0) / count;

I don’t see why I should use filter() or map(). It passed the test.

1 Like

I did not know I could blurred it out until I posted it (I saw it in one of your posts). I will try to keep it in mind. I myself hate finding a full solution when I am actually looking for a hint

This is because of order of operations. Multiplication and division always come before addition and substraction.

So…

// incorrrect order grouping
 "8.8" 2.2 // (0 + 8.8) / 4 = 2.2

// correct grouping
 "8.8" 2.2 // 0 + (8.8 / 4) = 2.2

hi there! very neat solve! :+1:
could you, please, explain why checking for the end of the watchList array, and returning the accumulator Obj if it’s not the end, is essential for the solution to work?

I’ve written a very similar almost-solution, but I was passing only 2 args (an accumulator and the current value) to reduce() and it was spitting out a NaN. ;/

thanks a lot!
ldo, lol #BrainFart =)

0 “8.8” 2.2 //(8.8)/4 = 2.2
2.2 “8.6” 4.35 //(8.8+8.6)/4 = 4.35
4.35 “9.0” 6.6 //(8.8+8.6+9.0)/4 = 6.6
6.6 “8.3” 8.675 //(8.8+8.6+9.0+8.3)/4 = 8.675

Actually not correct grouping as @JM-Mendez stated

The correct way is as follows:

0 “8.8” 2.2 //(8.8)/4 = 2.2
2.2 “8.6” 4.35 //2.2+(8.6/4) = 4.35
4.35 “9.0” 6.6 //4.35+(9.0/4) = 6.6
6.6 “8.3” 8.675 //6.6+(8.3/4) = 8.675

Hello guys. This is my solution

var filteredList = watchList.map(x => parseFloat(x["imdbRating"])).filter(x => x >= 8);

var averageRating = filteredList.reduce((a,b) => a+b)/(filteredList.length);

2 Likes

Hi, I did used Filter, Map and Reduce as mentioned.

// Add your code below this line
let filteredArray = watchList.filter(item => {
  return item.Director === "Christopher Nolan" && parseFloat(item.imdbRating) > 8;
});

let AllRatings = filteredArray.map(item => parseFloat(item.imdbRating));

let ratingsLength = AllRatings.length;

var averageRating = AllRatings.reduce((a,b)=>{
  return a + b / ratingsLength;
}, 0)

// Add your code above this line

Thank @randalldawson
I will stop show my solution in the future because our community

My solution below:

Solution

var filter = watchList.filter(function(director){
return director.Director == “Christopher Nolan”;
});

var sum = filter.reduce(function(prev, current){
return prev + parseFloat(current.imdbRating);
},0);

var averageRating = sum / filter.length;