I am running into a problem where, if two elements of an array are the same string value, then it produces problems in what gets printed to the console based on my current conditional logic, that looks like this:
const homeTeamScorers = ['John Smith', 'John Smith', 'Sam Jones'];
const homeTeamGoalTimes = [3, 10, 22];
for (let t of homeTeamGoalTimes) {
const timeIdx = homeTeamGoalTimes.indexOf(t);
for (let homeScorer of homeTeamScorers) {
const scorerIdx = homeTeamScorers.indexOf(homeScorer);
if (scorerIdx === timeIdx) {
console.log('home scorerIdx:', scorerIdx);
console.log('home goalTime: ', t);
console.log('home timeIdx:', timeIdx);
console.log('home scorer: ', homeScorer);
console.log('-----------');
}
}
}
This is what I see in the console (notice the second element group is repeating the first):
home scorerIdx: 0
home goalTime: 3
home timeIdx: 0
home scorer: John Smith
-----------
home scorerIdx: 0
home goalTime: 3
home timeIdx: 0
home scorer: John Smith
-----------
home scorerIdx: 2
home goalTime: 22
home timeIdx: 2
home scorer: Sam Jones
-----------
When what I want to see is this:
home scorerIdx: 0
home goalTime: 3
home timeIdx: 0
home scorer: John Smith
-----------
home scorerIdx: 1
home goalTime: 10
home timeIdx: 1
home scorer: John Smith
-----------
home scorerIdx: 2
home goalTime: 22
home timeIdx: 2
home scorer: Sam Jones
-----------
What am I missing here?