Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

73
Views
Javascript function in a object issue with infinite loop

Was trying to declare a function in my object but inside my code it is saying infinite loop within my code. I tried to declare it as a normal function but my IDE only wants it declared like it is below but yet it says infinite loop when I compile it.

let movie = {
  title: "Forrest Gump",
  director: "Robert Zemeckis",
  composer: "Alan Silvestri",
  budget: 55000000,
  boxOffice: 677900000,
  awards: [],

  totalRevenue: function () {
    let x = this.movie.boxOffice - this.movie.budget;
    return x;
  },
};

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Delete movie in your function:

totalRevenue: function() {
    let x = this.boxOffice - this.budget;
    return x;
},

Your code is error because it's trying to find an object called "movie" in this object which you don't have. Your function will work if you make your object like this:

let movie = {
    title: "Forrest Gump",
    director: "Robert Zemeckis",
    composer: "Alan Silvestri",
    budget: 55000000,
    boxOffice: 677900000,
    awards: [],
    movie: {
        boxOffice: 677900000,
        budget: 55000000
    },
    totalRevenue: function () {
        let x = this.movie.boxOffice - this.movie.budget;
        return x;
    },
};
about 4 years ago · Juan Pablo Isaza Report

0

In your code this belong to movie object, so remove movie from your function you can directly invoke boxoffice and budget because it is belong to the this

let movie = {
  title: "Forrest Gump",
  director: "Robert Zemeckis",
  composer: "Alan Silvestri",
  budget: 55000000,
  boxOffice: 677900000,
  awards: [],

  totalRevenue: function () {
    let x = this.boxOffice - this.budget;
    return x;
  },
};

movie.totalRevenue(); // will return 622900000

And one more thing your code is not going in infinite loop rather than it is unable to find movie inside this

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!