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

142
Vistas
Get javascript code and modify it to execute chainable

I want to change my code to execute chaining. this is my code:

let stepCount = {
    step: 0,
    up() {
        this.step++;
    },
    down() {
        this.step--;
    },
    showStep: function () {
       return this.step;
    }
};

How should I change it? I want to execute this code to get answer

stepCount.up().up().down().up();
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

you just need to return the current object from each function like this

let stepCount = {
  step: 0,
  up() {
    this.step++;
    return this;
  },
  down() {
    this.step--;
    return this
  },
  showStep() {
    console.log(this.step)
    return this;
  }
};

stepCount.up().up().down().showStep().down().showStep();

about 4 years ago · Juan Pablo Isaza Denunciar

0

This should do the job:

class Step {
    constructor(count) {
        this.step = count;
    }

    up() {
        this.step++;
        return this;
    }

    down() {
        this.step--;
        return this;
    }
}

console.log(new Step(5).up().up().step); // 7
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