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

SurveyJS custom survey software

JavaScript UI Library for Surveys and Forms

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

View demo

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay