Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

74
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda