Find index of array elements which sum together to equal target

Below is a working solution (very efficient) to the problem: “find index of array elements which sum together to equal target”

I have two questions:
1.How does this function rule out cases where an element of an array is added to itself to equal the target?

  1. How does “map.set(nums[i], i);” end up returning an array with the index of the two elements separated by a comma?
function bebo(nums, target) {
 let map = new Map;
    for (var i = 0; i < nums.length; i++) {
        let complement = target - nums[i];
        if (map.has(complement)) {
            return [map.get(complement), i]
        }
        map.set(nums[i], i);
    }
  
}

bebo([2,11,7,15],9)

Thanks in advance : )

  1. How does this function rule out cases where an element of an array is added to itself to equal the target?

The question isn’t clear to me. Are you asking why this:

bebo([2, 1, 3], 4)

… doesn’t just return [0, 0]? Because when it checks that 2, it hasn’t stored it into the map yet so it doesn’t see 2 in the map yet`. The set method is called after the check.

When in doubt, step through it. You can put a bunch on log statements in there or write it out on paper.