I'm writing a function for a game called TwilioQuest. The function neeps to loop through each item in the freightItems array (already defined by TwilioQuest) and count each time the item 'contraband' is noticed. I created an array after the function to test the code.
The problem I'm having is that the function doesn't seem to be able to discriminate between the item 'contraband' and any other item. When the contrabandCount is returned, it displays "5" instead of the expected "2". Can anyone help me understand where I went wrong?
Here's my code:
function scan(freightItems) {
let contrabandCount = 0;
freightItems.forEach(function(freightItems) {
if ('contraband') contrabandCount++;
});
return contrabandCount;
}
const numItems = scan(['dog', 'contraband', 'cat', 'zippers',
'contraband'
]);
console.log('Number of "contraband": ' + numItems)
if ('contraband')as you need to check iffreightItem(the argument in your forEach callback) equals'contraband'Array.forEach?