Build an Inventory Management Program - Build an Inventory Management Program

Tell us what’s happening:

good day! can anyone help me to fix mistakes, please. I don’t understand why system won’t accept this lab

Your code so far

const inventory = [];

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

function addProduct(product) {
  const index = findProductIndex(product.name);
  if (index === -1) {
    inventory.push(product);
    console.log(`${product.name} added to inventory`)
  } else {
      inventory[index].quantity += product.quantity;
      console.log(`${product.name} quantity updated`);
  }
}

function removeProduct(name, quantity) {
  const index = findProductIndex(name);
  if (index === -1) {
    console.log(`${name} not found`)
    return;
  } 

  const currentQuantity = inventory[index].quantity;
  const total = currentQuantity - quantity;

  if (currentQuantity < quantity) {
    console.log(`Not enough ${name} available, remaining pieces: ${inventory[index].quantity}`);
    return;
  }
  
  if (total === 0) {
    inventory.splice(index, 1);       
  } else {
    inventory[index].quantity = total 
  }
  console.log(`Remaining ${name} pieces: ${total}`);
}



Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 YaBrowser/26.6.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

Please review the instructions to make sure you are implementing them correctly. For example, is your code in line with this instruction?

  1. You should declare an empty array named inventory that will store product objects having a key name with the value of a lowercase string, and a key quantity with the value of an integer.
I log every function on console, function work correctly
8. addProduct({name: "FLOUR", quantity: 5}) should log flour quantity updated when an object having name equal to "flour" is found in the inventory array.
9. addProduct({name: "FLOUR", quantity: 5}) should push {name: "flour", quantity: 5} to the inventory array when no object having name equal to "flour" is found in the inventory.
10. addProduct({name: "FLOUR", quantity: 5}) should log flour added to inventory when no object having name equal to "flour" is found in the inventory.
12. removeProduct("FLOUR", 5) should log flour not found when no object having name equal to "flour" is found in the inventory array.
14. removeProduct("FLOUR", 5) should log Remaining flour pieces: 15 to the console, when inventory is equal to [{name: "flour", quantity: 20}, {name: "rice", quantity: 5}].
16. removeProduct("FLOUR", 10) should log Not enough flour available, remaining pieces: 5 when inventory is equal to [{name: "flour", quantity: 5}, {name: "rice", quantity: 5}].

Have you updated your code? If so, please post it.

no, i haven’t updated it yet

Should there ever be an uppercase name key in an inventory array object? If you pass FLOUR to findProductIndex, should it find the object?

thank u very much, u help fix code