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

158
Visualizações
How this javaScript code is work? after if condition

i do not understood, how this code working after if(y in hash) most complicated for me is hash is empty and i do not push any value in hash so what will happened behind the scene and y in hash what its meaning?

var twoSum = function(nums, target) {

  const hash = {}
  // console.log(hash)
  
  for (const i in nums) {
    const x = nums[i];
    const y = target - x
    // console.log(y)
    if (y in hash)
      return [i, hash[y]]
    hash[x] = i
  }

}

let arr = [2, 3, 4, 5, 6]
console.log(twoSum(arr, 11))

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

hash is empty and I do not push any value

hash[x] = i part add values to hash, because it is out of if(y in hash) condition

y in hash what it's meaning?

As Nick Parsons mentioned in a comment "it's checking if the key stored in y is a key in the hash object (or its prototype chain)"

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