Build an Inventory Management Program - Build an Inventory Management Program

Tell us what’s happening:

I am passing everything but Step 12. I’m getting a TypeError: Cannot read properties of undefined (reading ‘name’). I’ve read the documentation but I’m not seeing why it’s not working. (It’s probably something little, it usually is.)

Your code so far

const inventory = [];

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

function addProduct(obj) {
  obj.name = obj.name.toLowerCase();
  let productIndex = findProductIndex(obj.name);

  if (productIndex === -1) {
    inventory.push(obj);
    console.log(`${obj.name} added to inventory`)
  }

  else {
    inventory[productIndex].quantity += obj.quantity;
    console.log(`${obj.name} quantity updated`);
  }
}

function removeProduct (name, quant) {
  let productIndex = findProductIndex(name);
  let productName = inventory[productIndex].name;
  let productQuant = inventory[productIndex].quantity;

  if (productIndex == -1) {
    console.log(`${name.toLowerCase()} not found`);
    return;
  }
  else if (productQuant - quant > 0) {
    inventory[productIndex].quantity = inventory[productIndex].quantity - quant;
    console.log(`Remaining ${productName} pieces: ${inventory[productIndex].quantity}`)
  }
  else if (productQuant - quant === 0) {
    inventory.splice(productIndex, 1);
  }
  else if (productQuant - quant < 0) {
    console.log(`Not enough ${productName} available, remaining pieces: ${productQuant}`)
  }
}

console.log(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/146.0.0.0 Safari/537.36

Challenge Information:

Build an Inventory Management Program - Build an Inventory Management Program

Found my own error! Product not existing has nothing to do with product quantity, split the two tests in half, declare the two variables related to name and quantity after initial if statement, problem solved!

Really? I opened the browser’s console, which reported an error in your removeProduct function. Log productIndex after you try to get it. What if the product name wasn’t found in inventory?