I have an issue with my removeProduct function passing test 13.
13. removeProduct("FLOUR", 5)
should subtract 5
from the quantity
value of the object having name
equal to "flour"
inside the inventory
array.
Here is the code:
function removeProduct(itemName, itemCount) {
let product = itemName.toLowerCase();
let index = findProductIndex(itemName);
if (index === -1) {
console.log(`${product} not found`);
} else if (index !== -1) {
let currentCount = inventory[index].quantity;
if (currentCount === itemCount) {
inventory.splice(index, 1);
} else if (itemCount > currentCount) {
console.log(`Not enough ${product} available, remaining pieces: ${currentCount}`);
} else {
currentCount -= itemCount;
console.log(`Remaining ${product} pieces: ${currentCount}`);
}
}
}
When I test the function with console.log()
below:
inventory.push({name: "flour", quantity: 10});
inventory.push({name: "first", quantity: 10});
inventory.push({name: "second", quantity: 20});
inventory.push({name: "third", quantity: 30});
removeProduct("FLOUR", 5);
removeProduct("First", 5);
removeProduct("SEcond", 5);
removeProduct("THIrd", 5);
It logs:
Remaining flour pieces: 5
Remaining first pieces: 5
Remaining second pieces: 15
Remaining third pieces: 25
I don’t understand why it isn’t passing. Could someone help me?