Build an Inventory Management Program - Build an Inventory Management Program Item 13

Tell us what’s happening:

I am now having an issue with passing Item 13. Please help me figure this out. It says "removeProduct(“FLOUR”, 5) should subtract 5 from the quantity value of the object having name equal to “flour” inside the inventory array. It is probably something simple I have missed.

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 removeProductOLD(productName, productQuantity) {

  console.log('product name is ' + productName);

  let lowerName = productName.toLowerCase();
  
  let index = findProductIndex(lowerName);

  if (index === -1) {
    console.log(`${lowerName} not found`);
    return;
  }
  
  console.log(productName + ' where am i? ' + productQuantity + ' ' + lowerName + ' and index is ' + index )

  let realQuantity = inventory[index].quantity;

  console.log(productName + ' real qty ' + realQuantity);
  
  newQuantity = realQuantity - productQuantity;
  
  console.log('New qty is ' + newQuantity);
  

}

function removeProduct(productName, productQuantity) {
  let lowerName = productName.toLowerCase();
  let index = findProductIndex(lowerName);

  if (index === -1) {
    console.log(`${lowerName} not found`);
    return;
  }

  let realQuantity = inventory[index].quantity;

  if (productQuantity === realQuantity) {
    inventory.splice(index, 1);
  } else if (productQuantity > realQuantity) {
    console.log(`Not enough ${lowerName} available, remaining pieces: ${realQuantity}`);
  } else {
    /* inventory[index].quantity -= productQuantity; */
	productQuantity = realQuantity - productQuantity;
    console.log(`Remaining ${lowerName} pieces: ${productQuantity}`);
  }
}

inventory.push({name: "flour", quantity: 20});
inventory.push({name: "first", quantity: 10});
inventory.push({name: "second", quantity: 20});
inventory.push({name: "third", quantity: 30});

removeProduct("FLOUR", 5);
removeProduct("First", 5);
removeProduct("SEcond", 5);
removeProduct("THIrd", 5); 

Your browser information:

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

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

Hi @admagee63,

Are you changing the quantity in inventory or just changing the value of the productQuantity parameter?

Happy coding

That is where I am confused. Step 16 works fine but this one doesn’t and they are similar.

Test #16 does not involve changing the quantity of an inventory item.

So I need to just change the value of the productQuantity parameter instead of changing the quantity in inventory?

That’s exactly opposite of what my initial reply to you was attempting to point out.

Oh duh. I don’t know why I said that backward! Too early in the day I guess, lol

No worries. Happy coding.