mtf didn’t say that the indexes i
and j
were interchangeable. Rather,
mutualFollowers.push(bobsFollowers[i]);
// does the same thing as
mutualFollowers.push(tinasFollowers[j]);
// Either of the two statements above will push
// the correct element to mutualFollowers
Swapping the indexes doesn’t work. The if condition is not checking whether the indexes of the two arrays are the same. The condition is checking whether the elements at the respective indexes of the two arrays are the same.
// Not correct because j is being used to iterate over tinasFollowers array
mutualFollowers.push(bobsFollowers[j]);
// Not correct because i is being used to iterate over bobsFollowers array
mutualFollowers.push(tinasFollowers[i]);
Sheer luck for first one even though the code is incorrect.
// You wrote:
const bobsFollowers = ["Gary", "Gordon", "George", "Gregory"];
const tinasFollowers = ["Sally", "Gordon", "Gregory"];
"Gordon"
is at index 1
of both arrays, so swapping i
and j
doesn’t cause a problem by coincidence. But "Gregory"
is at index 3
of bobsFollowers
and at index 2
of tinasFollowers
, so swapping i
and j
does make a noticeable difference. There is no index 3
in tinasFollowers
, so pushing tinasFollowers[3]
would push undefined
to mutualFollowers
.
If the arrays were:
const bobsFollowers = ["Gordon", "Gary", "George", "Gregory"];
const tinasFollowers = ["Sally", "Gordon", "Gregory"];
then swapping i
and j
as per your posted code would cause mutualFollowers
to be
[ 'Sally', undefined ]
which is clearly incorrect.
Swapping i
and j
breaks the logic of your code and makes it incorrect.