DEV Community

Abhinav
Abhinav

Posted on

JS practice question

I was going through some lectures of JS to get refresher. So just thought of posting my learning here

const favouritesMovies = {
  "Matrix": {
      imdbRating: 8.3,
      actors: ["Keanu Reeves", "Carrie-Anniee"],
      oscarNominations: 2,
      genre: ["sci-fi", "adventure"],
      totalEarnings: "$680M"
  },
  "FightClub": {
      imdbRating: 8.8,
      actors: ["Edward Norton", "Brad Pitt"],
      oscarNominations: 6,
      genre: ["thriller", "drama"],
      totalEarnings: "$350M"
  },
  "Inception": {
      imdbRating: 8.3,
      actors: ["Tom Hardy", "Leonardo Dicaprio"],
      oscarNominations: 12,
      genre: ["sci-fi", "adventure"],
      totalEarnings: "$870M"
  },
  "The Dark Knight": {
      imdbRating: 8.9,
      actors: ["Christian Bale", "Heath Ledger"],
      oscarNominations: 12,
      genre: ["thriller"],
      totalEarnings: "$744M"
  },
  "Pulp Fiction": {
      imdbRating: 8.3,
      actors: ["Sameul L. Jackson", "Bruce Willis"],
      oscarNominations: 7,
      genre: ["drama", "crime"],
      totalEarnings: "$455M"
  },
  "Titanic": {
      imdbRating: 8.3,
      actors: ["Leonardo Dicaprio", "Kate Winslet"],
      oscarNominations: 13,
      genre: ["drama"],
      totalEarnings: "$800M"
  }
}
Enter fullscreen mode Exit fullscreen mode

Here we have a nested object.
Our objective is to Find all the movies with total earnings more than $500M.

Here is my solution:

const movieEarning = (favouritesMovies) =>{
  const moviesWithEarningMoreThan500 = [];

  for(const movie in favouritesMovies){
    // console.log(favouritesMovies[movie].totalEarnings.replace('$',''));
    if (parseInt(favouritesMovies[movie].totalEarnings.replace('$','')) > 500){
      // console.log(favouritesMovies[movie]);
      favouritesMovies[movie].name = movie
      moviesWithEarningMoreThan500.push(favouritesMovies[movie])
    }
  }
  // console.log(moviesWithEarningMoreThan500);
  return moviesWithEarningMoreThan500
}
Enter fullscreen mode Exit fullscreen mode

As a beginner i like using logs whenever i get stuck.

People reading this please leave mention other topics that are necessary for learning Js

Top comments (0)