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

133
Vistas
Sum in constructor

I'm trying to solve the problem. When the user enter two numbers via ** prompt **, after the display shows the final result. Simple constructor, but this code only accepts the first value, I cannot force it to take the second one too, in order for sum both values

function Num (firstNum) {
    this.firstNum = firstNum;
    this.read = function() {
        this.value = this.x + this.firstNum; {
            return this.x = +prompt('a');
        }
    };
}
let num = new Num(10); 
num.read(); 
num.read(); 

alert(num.value); 
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

solve

function Num (firstNum) {
    this.value = firstNum;
    this.read = function() {
        this.value += +prompt('a?', 0);
    };
}

let num = new Num(10); 
num.read(); 
num.read(); 

alert(num.value);  
about 4 years ago · Juan Pablo Isaza Denunciar

0

As other commenters have suggested you should probably edit your question to make it clearer. But if I had to take a guess here's my answer:

Lookup "curry functions" or "partial application". You can basically use closures to stash the value from the first prompt until you receive the value from the second.

    const sumTwo = firstNum => secondNum => firstNum + secondNum;

    // then when you want to use it;
    const plusTen = sumTwo(prompt(10);

    const resultA = plusTen(prompt(2)); // this will be 12
    const resultB = plusTen(prompt(5)); // this will be 15
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