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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay