Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

132
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!