Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

159
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda