Build an Inventory Management Program - Build an Inventory Management Program

Tell us what’s happening:

Please my code can’t pass test 12 && 16 please I need help. Please don’t provide the correct code, ask me questions that can help me solve it myself

Your code so far

const inventory  = []
const findProductIndex = (productName)=>{
  for (let i = 0; i < inventory.length; i++){
    if (inventory[i].name === (productName.toLowerCase())){
      return i
    }
}
return -1
}

const  addProduct = (productObj)=>{
let product = productObj;
const index = findProductIndex(product.name);
if (index !== -1){
   inventory[index].quantity += product.quantity;
console.log(product.name.toLowerCase() + " quantity updated");

}
 else if (index === -1){
const unModified = productObj
const productQuantityValue = unModified.quantity
const productName = unModified.name.toLowerCase()
const modified = {name: productName, quantity: productQuantityValue}
  inventory.push(modified)
   console.log(`${product.name.toLowerCase()} added to inventory`);
   }
}

const removeProduct = (productName, num)=>{
  const index = findProductIndex(productName)

  if (index === -1 ){
console.log(`${inventory[index].name.toLowerCase()} not found`)
  }

if (index !== -1 && inventory[index].quantity !== 0){
 inventory[index].quantity -= num
 console.log(`Remaining ${productName.toLowerCase()} pieces: ${ inventory[index].quantity}`)
}

if (index !== -1 && inventory[index].quantity === 0){
inventory.splice(index, 1)
}

if (num > inventory[index].quantity){
 console.log(`Not enough ${productName.toLowerCase()} available, remaining pieces: ${inventory[index]. quantity}`)
  }
}





Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36 OPR/90.0.0.0

Challenge Information:

Build an Inventory Management Program - Build an Inventory Management Program
https://www.freecodecamp.org/learn/full-stack-developer/lab-inventory-management-program/build-an-inventory-management-program

add this code to check

inventory.length = 0;
inventory.push({name: 'flour', quantity: 15});
inventory.push({name: 'rice', quantity: 10});
inventory.push({name: 'sugar', quantity: 5});
removeProduct("SALT", 2);
removeProduct("Spam", 1);

and then this code

inventory.length = 0;
inventory.push({name: 'flour', quantity: 15});
inventory.push({name: 'rice', quantity: 10});
inventory.push({name: 'sugar', quantity: 5});
removeProduct("flour", 20);
removeProduct("RICE", 20);
removeProduct("Sugar", 20);

Ok, thanks, on it I will update you soon

Thank you. I have solved it .

I came up with the right logic. :heart: