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

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

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

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

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