Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

77
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda