I've encountered an issue while trying to develop a function that iterates through each item in an array and records the index of a specific item when found. In this particular function, whenever the item 'contraband' is detected during the loop, the program should note its index by using the .push() method to add it to an array named 'contrabandIndexes'. However, there seems to be an error within the command or another section of the code since executing the function with a sample array results in an empty output.
Can you help pinpoint where I may have made a mistake?
function scan(freightItems) {
let contrabandIndexes = [];
freightItems.forEach(function(freightItem, index) {
if (freightItem === 'contraband') {
contrabandIndexes.push(index);
}
});
return contrabandIndexes;
}
const indexes = scan(['dog', 'contraband', 'cat', 'zippers', 'contraband']);
console.log('Contraband Indexes: ' + indexes);