Build an Inventory Management Program - Build an Inventory Management Program

Tell us what’s happening:

TypeError: “Cannot read properties of undefined (reading ‘quantity’)”

having issues adding to an existing quantity of an object

Your code so far

let inventory = [
  
];



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


function addProduct(obj) {
 
  let productStr = obj.name.toLowerCase();
  let productIndex = findProductIndex(productStr);
  
  if (productIndex !== -1) {
    inventory[productIndex].quantity += obj.quantity;
    console.log(`${productStr} quantity updated`);
  } else {
    inventory.push({ name: productStr, quantity: obj.quantity });
    console.log(`${productStr} added to inventory`);
  }
}


console.log(findProductIndex("flour"));
console.log(findProductIndex("floUr"));
addProduct({name: "FLOUR", quantity: 5});
addProduct({name: "FLOUR", quantity: 5});
console.log(inventory);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36

Challenge Information:

Build an Inventory Management Program - Build an Inventory Management Program

You should create a function named findProductIndex that takes the product name as its argument and returns the index of the corresponding product object inside the inventory array.

What should findProductIndex() return?

the name of the object in lowercase or -1

Where are you reading that you should return the name of the object?