I am solving the following leetcode question and have the solution below
const twoSum = (numbers, target) => {
let map = {}
let result = []
for (let i = 0; i < numbers.length; i++) {
let complement = target - numbers[i]
if (map[complement] === undefined) {
map[numbers[i]] = i
} else {
result[0] = map[complement] + 1
result[1] = i + 1
}
}
return result
};
If I replace map[complement] === undefined
with !map[complement]
I return an empty array. In my mind both should return true
. Why does the latter breaks my code?