Build an Inventory Management Program - Build an Inventory Management Program

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

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-inventory-management-program/66d75dd0aa65a71600dc669b.md at main · freeCodeCamp/freeCodeCamp · GitHub

Hi @Emmysifire,

Does it make sense to do all that processing before checking to see if the product exists in inventory? And why aren’t you using your findProductIndex function to access the product inventory object directly rather than looping over inventory again?

Happy coding

I know some lines of my code is redundant. The for loop is needed for the quantity comparison which i intend not to use a method for.

else if(inventory[i].name === nameInLowerCase && (inventory[i].quantity - quantityOfProduct) === 0){
inventory.splice(i, 1);
}