Why test isn't passing

Tell us what’s happening:
My code isn’t passing the last test. I also don’t understand what last test means.

// The global variable

const 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, Elliot 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"

  }

];

var arr = [];

function getRating(watchList) {

  // Only change code below this line

  let averageRating;

  

watchList.map(function(item){

  if(item['Director'] === 'Christopher Nolan'){

    arr.push(+item['imdbRating']);

  }

})

averageRating = arr.reduce((a,b) => a + b,0)/ arr.length

  // Only change code above this line

  return averageRating;

}

console.log(getRating(watchList));


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.127 Safari/537.36

Challenge: Use the reduce Method to Analyze Data

Link to the challenge:

Hello there @vilgax04.

You do not seem to have this arr variable defined in your code hence pushing items into an array that doesn’t exit will naturally result into an error.

What you would want to return from your map would be a filtered array of Christopher Nolan’s imdbRatings after which you would now use the reduce method but before getting the average you would need to convert the values of the imdbRating into integers because they are stored as strings.

The way I would approach this challenge is to first get the list of all of imdbRating of Christopher Nolan’s movies using the filter and map method and assign that to a variable like this:

const imdbRatings = watchList.filter( movie => 
    movie.Director === "Christopher Nolan"
  ).map( movies => 
    movies.imdbRating;
  );

After which you can now use the reduce method to sum up all the ratings and then calculate the average.

averageRating = imdbRatings.reduce((acc, curr) => {
   acc += parseFloat(curr);
    return acc; 
  }, 0) / imdbRatings.length;

I made use of the fat arrow functions and implicit return which you seem to be knowledgeable in and I hope this helps view how you could have solved the challenge. There are still other methods that are shorter I’d advice you try to come up with a different solution afterwards.

Happy coding :+1:!

1 Like

Thank you for your reply

I passed the challenge by just moving ‘arr’ inside the function, but I wanted to ask what difference it makes if i create it as global variable,
and what you meant by this

here is my code:

function getRating(watchList) {

  // Only change code below this line

  let averageRating;
  var arr = [];


watchList.map(function(item){

  if(item['Director'] === 'Christopher Nolan'){

    arr.push(+item['imdbRating']);

  }

})

averageRating = arr.reduce((a,b) => a + b,0)/ arr.length

  // Only change code above this line

  return averageRating;

}

console.log(getRating(watchList));

I am new to programming, and JavaScript is my first programming language which I am learning from FCC on my own, that’s why I thing I don’t undderstand these small things.

This is not a good way to use a map. A map makes a new array out of an old array. Here you are throwing array that the map method creates and instead using the map to loop over the array. This technically works but is a huge anti-pattern.

1 Like

The scope of the variable matters. In most challenges in the FCC JS Curriculum the test specifically gives designated sections for you to write your codes so anything outside of that section will cause the test to fail.

You’re currently on Functional Programming so I believe you have some knowledge about variable scopes.

Variables inside of a function can not to be accessed outside because they have block scope while variables outside of the function are global and they can be accessed from anywhere and sometimes modified especially if they’re declared with var. Also, try not to use var keyword for your variables because this makes your variables loose as var variables can be changed or reassigned so except that is your aim, use let and const provided in ES6.

The arr was accessed by the function but the test will only pass if the variable has a block scope(i.ie. the variable must be declared inside the designated area) and for the test the arr variable wasn’t in the correct scope so it failed.

I think I might have missed the fact you declared the arr outside of the function my apologies.

:point_up_2: This is fine. JavaScript was also my first language which I learnt by myself and trust me it takes some time to understand what to do and how to do it. But once you get a hang of the concept, coming up with algorithm or solutions becomes a piece of cake. Keep practicing everyday.

I hope I was able to answer your questions. Happy coding :+1:!

1 Like

Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2
1 Like

Thank you for your reply
I can understand what you are trying to say, and you are absolutely right.

Thank you for your reply

I will try to do this

Thanks for sharing your story it motivates me.

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