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

184
Views
Finding the first pair that sums to a given integer
    function sumPairs(ints, s) {
  
  let result = [];
  
  for(let i = 0; i < ints.length; i++){
    for(let j = 0; j < ints.slice(i); j++){ 
      (ints[i] + ints[j] === s && result.length === 0) ?  result.push(ints[i], ints[j]) : 0;
    } 
  } return result;
}
sumPairs([1, 2, 3, 4, 5, 6], 10) //returns [6, 4] instead of [4,6]?

How do I fix this so that the function returns the first pair that add up to s, the second argument? I added the result.length === 0 condition in the if statement a further update would be prevented?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Just iterate over the array using for..of loop and use Set to achieve the desired result.

function sumPairs(ints, s) {
  const dict = new Set();

  for (let num of ints) {
    if (dict.has(s - num)) return [s - num, num];
    else dict.add(num);
  }
}
console.log(sumPairs([1, 2, 3, 4, 5, 6], 10));

Iterations:

Iteration 1 :=> num = 1 and dict is empty

dict doesn't have 9 (because of s - num i.e 10 - 1) so add num i.e 1 to dict.

Iteration 2 :=> num = 2 and dict has 1

dict doesn't have 8 (because of s - num i.e 10 - 2) so add num i.e 2 to dict

Iteration 3 :=> num = 3 and dict has 1, 2

dict doesn't have 7 (because of s - num i.e 10 - 3) so add num i.e 3 to dict

Iteration 4 :=> num = 4 and dict has 1, 2, 3

dict doesn't have 6 (because of s - num i.e 10 - 4) so add num i.e 4 to dict

Iteration 5 :=> num = 5 and dict has 1, 2, 3, 4

dict doesn't have 5 (because of s - num i.e 10 - 5) so add num i.e 5 to dict

Iteration 6 :=> num = 6 and dict has 1, 2, 3, 4, 5

dict have 4 (because of s - num i.e 10 - 6) so return [s - num, num] i.e [4, 6]

about 4 years ago · Juan Pablo Isaza Report

0

I think you might be overcomplicating things a little. There's no need to slice() or to push(), and there's no need to keep on looping after a result has been found:

function sumPairs(ints, s) {
  for (let i = 0; i < ints.length; i++) {
    for (let j = i; j < ints.length; j++) {
      if (ints[i] + ints[j] == s) {
        return [ints[i], ints[j]];
      }
    }
  }
}

console.log(sumPairs([1, 2, 3, 4, 5, 6], 10));

about 4 years ago · Juan Pablo Isaza Report

0

As @robby-cornelissen said there's no need to slice. And there's no need for push too as you can return the result simply.

When you do ints.slice(i) you get an array from that position.
You are comparing an int within an array in the second loop.
It didn't enter the second loop until "i === 5" which is in the ints array a [6]. Notice it didn't enter the second until you get an array of one element. Keep in mind 6 == [6] and 1<[6] and 2<[6] etc

Then it goes incrementing the value of j until you get the position of j that gets the value of ints which, summed by the value of the position i results on s.

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!