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

160
Views
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 answers
Answer question

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