Tell us what’s happening:
I cannot pass task 12. Everything is running and yet the task can’t be passed.
Your code so far
const inventory = [];
function findProductIndex(productName){
let nametoLowerCase = productName.toLowerCase();
let indexOfChar = -1;
for(let i=0; i<inventory.length; i++){
if(inventory[i].name === nametoLowerCase){
indexOfChar = i
}
}
return indexOfChar
}
function addProduct(objectElement){
let productName = objectElement.name.toLowerCase();
for(let i=0; i<inventory.length; i++){
if(inventory[i].name === productName){
inventory[i].quantity += objectElement.quantity;
console.log(`${productName} quantity updated`);
return;
}
}
inventory.push(
{
name: productName,
quantity: objectElement.quantity
})
console.log(`${productName} added to inventory`)
}
addProduct({name: "Rice", quantity: 7});
addProduct({name: "Rice", quantity: 5});
function removeProduct(nameOfProduct, quantityOfProduct){
let nameInLowerCase = nameOfProduct.toLowerCase()
for(let i=0; i<inventory.length; i++){
if(inventory[i].name === nameInLowerCase && (inventory[i].quantity - quantityOfProduct) > 0){
inventory[i].quantity -= quantityOfProduct;
console.log(`Remaining ${nameInLowerCase} pieces: ${inventory[i].quantity}`);
}
else if(inventory[i].name === nameInLowerCase && (inventory[i].quantity - quantityOfProduct) === 0){
inventory.splice(i, 1);
}
else if(inventory[i].name === nameInLowerCase && (inventory[i].quantity - quantityOfProduct) < 0){
console.log(`Not enough ${nameInLowerCase} available, remaining pieces: ${inventory[i].quantity}`);
}
else if(findProductIndex(nameOfProduct) < 0){
console.log(`${nameInLowerCase} not found`);
}
}
}
removeProduct("FLOUR", 5);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/151.0.0.0 Safari/537.36
Challenge Information:
Build an Inventory Management Program - Build an Inventory Management Program