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

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

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

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 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