Build an Inventory Management Program - Build an Inventory Management Program

Tell us what’s happening:

I have this done, all the test passed. Only 1 through me an error:

  1. removeProduct(“FLOUR”, 5) should log flour not found when no object having name equal to “flour” is found in the inventory array.

I cant understand why doesnt work!

Your code so far

let inventory = [];

function findProductIndex(productName){
  let index = inventory.findIndex(obj => obj.name === productName.toLowerCase());
  return index;
}


function addProduct(productObject){
  let newObjectLowerCase = productObject.name.toLowerCase();
  let index = inventory.findIndex(item => item.name === newObjectLowerCase);

  if(index !== -1){
    inventory[index].quantity += productObject.quantity;
    console.log(`${newObjectLowerCase} quantity updated`);
  } else {
    inventory.push({name:newObjectLowerCase, quantity:productObject.quantity});
    console.log(newObjectLowerCase + " added to inventory");
  }
}


function removeProduct(productName, productQuantity){
  let lowerName = productName.toLowerCase();
  let index = findProductIndex(productName);
  let realQuantity = inventory[index].quantity;

  if(index === -1){
    console.log(lowerName + " not found");
    return;
  }else if(index !== -1){
    if(productQuantity === realQuantity){
      inventory.splice(index, 1);
    }else if(productQuantity > realQuantity){
      console.log(`Not enough ${lowerName} available, remaining pieces: ${inventory[index].quantity}`);
    }else if(productQuantity < realQuantity){
      inventory[index].quantity -= productQuantity;
      console.log(`Remaining ${lowerName} pieces: ${inventory[index].quantity}`)
    }
  }
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36

Challenge Information:

Build an Inventory Management Program - Build an Inventory Management Program

Have you checked what happens when removeProduct("FLOUR", 5); is called? You can add it below your code for testing.

You get a type error when removeProduct(“FLOUR”, 5)is called as it can’t read the undefined property of ‘quantity’.

Optional chaining where quantity is accessed before the function returns will fix this. Add optional chaining here:

let realQuantity = inventory[index].quantity;

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

But i dont understand what its the problem with the code

Sounds like something is trying to be accessed but it doesn’t exist.

You can use console.log() to send the variables that you access in the remove function to see if they exist/what they contain before you actually use them.

Once you understand the place the error occurs you can handle that situation

Im finally understand. I was trying to find a quantity of an “-1” index. I have to move the realQuantity variable inside the condition when the index variable is true.
here it is the correct way:

removed by mod

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

That was definitely a tricky one. I didn’t see your other solution before it was removed - good to finally understand. It took me ages to pass this challenge.