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

228
Visualizações
Sum of 2 elements in array - Algorithm - JavaScript

I'm working on JavaScript algorithms and could use some help. What am I doing wrong exactly?

// Given an array of arr, positive integers, and another number X.

// Determine whether or not there exist two elements in arr whose sum is exactly X.

function keyPair(arr, x){
    var sum=false;
    var key=0;
    var temp=0;
    for(var i=0;i<=arr.length;i++){
        while(sum=false){
            key=arr[i];
            arr[i]=temp;
            temp=key;
        }
        if(temp+arr[i]==x){
            sum=true;
        }
    }
    console.log(sum);
}
keyPair([1,2,4,3,6], 4);
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

I think this one deserves an explanation:

sum=false is an assignment statement. As an expression, an assignment statement is evaluated as undefined (regardless of the value assigned). So while(sum=false) is actually while(undefined), which is interpreted as while(false).

about 4 years ago · Juan Pablo Isaza Relatório

0

Your mistake was - as explained multiple times - the wrong comparison operator. I took the liberty of rewriting your code involving a .reduce() call:

function keyPair(arr,sum){
 return arr.reduce((a,c,j)=>{
  let i=arr.indexOf(sum-c);
  if (i>-1 && j<i) a.push([c,arr[i]]);
  return a;}, []);
}
console.log(keyPair([1,2,4,3,6], 5));

about 4 years ago · Juan Pablo Isaza Relatório

0

I'm not going to repeat what has been already said about your code. But even if you're starting coding in JS, it pays for you to start using JS built-ins that will save you a lot of time:

// Determine whether or not there exist two elements in arr whose sum is exactly X.

So you're only interested in whether they exist, not in which are those:

const keyPair = (arr,value) => 

// some: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

// slice: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

arr.some( (x,i) => arr.slice(i).some( y => x + y === value));


console.log(keyPair([1,2,4,3,6], 4))
console.log(keyPair([1,1,1,1,1], 4))

With some you check if there is an x+y sum that equals to your desired value

With slice you omit the portion of the array that you don't need to check

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