Just starting out with JavaScript and I've been learning for a couple of weeks, putting in about 8 hours so far. I know there's a similar question out there with a good answer, but I really want to understand why my approach isn't working.
TASK: The goal of this function is to take an array of strings as input. Your scan function should iterate through all the strings in the array, examining each one using boolean logic.
If a string in the input array matches the value 'contraband', the index of that item should be added to an output array. Once you have scanned the entire input array, return the output array containing the indexes of suspicious items.
For example, if the input array is:
['contraband', 'apples', 'cats', 'contraband', 'contraband'] Your function should return:
[0, 3, 4] This list shows the positions of all contraband strings within the input array.
function scan(freightItems) {
let contrabandIndexes = [];
for (let index in freightItems)
if (freightItems == 'contraband') contrabandIndexes.push(index);
return contrabandIndexes;
}
const indexes = scan(['dog', 'contraband', 'cat', 'zippers', 'contraband']);
console.log('Contraband Indexes: ' + indexes); // should be [1, 4]
Currently, I'm getting the output "Conrtraband Indexes:" instead of "Contraband Indexes 1, 4"
I would appreciate any help or guidance on this issue!