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

121
Visualizações
Why is this Javascript code stopping execution in my for loop?

I'm learning Javascript and wrote a small program. I have a method inside my object with a for loop. Why is my console showing output when my fuel value is at 5. It should still iterate through the loop right?

    let car = {

    make : "Chevy",
    year : "2022",
    model : "Malibu",
    fuel : 0,
    started : false,

    start: function(){
        this.started = true;
    },

    stop: function(){
        this.started = false;
    },

    drive: function(){
        if(this.fuel > 0){
            if(this.started){
              for(let i = 0; i < this.fuel; i++){
                console.log(`Enjoying the drive in my ${this.make} --${this.model}`);
                console.log(`Fuel before deduction ${this.fuel}`);
                this.fuel = this.fuel - 1;
                console.log(`Fuel after drive = ${this.fuel}`);
              }
            }else{
                console.log(`You need to start the car to be able to drive`);
                this.stop();
            }
        }else{
            console.log("You need to fuel the car to be able to dirve");
            this.stop();
        }
    },

    addFuel: function(amount){
        this.fuel += amount;

        console.log(`Fuel Added ${this.fuel}`);
    }
};

car.drive();

car.addFuel(10);

car.drive();

car.start();

car.drive();
almost 4 years ago · Santiago Trujillo
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