Build an Inventory Management Program Step 13- Build an Inventory Management Program

Tell us what’s happening:

I am still stuck on Step 13 not passing. I am at my wit’s end. Please help me figure out where I went wrong. I keep getting the message “removeProduct(“FLOUR”, 5) should subtract from the quantity value of the object having name equal to “flour” inside the inventory array.” Where am I going wrong? Thanks!

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) {

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

  if (index === -1) {
    console.log(`${lowerName} not found`);
    return;
  }
 
  let realQuantity = inventory[index].quantity;
  
  newQuantity = realQuantity - productQuantity;

}

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,

Is this changing the quantity of the item inside the inventory array?

Happy coding

I must be dense but I just don’t understand what you are asking. I have been trying to figure this out for a week. I just feel so stupid right now. :frowning:

Here, you’ve commented out the code that actually updates the inventory array with the new quantity. Just uncomment that and your code should pass.

I am still getting the step 13 error message “removeProduct("FLOUR", 5) should subtract 5 from the quantity value of the object having name equal to "flour" inside the inventory array.” I just don’t understand this.

please post your updated code

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) {

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

  if (index === -1) {
    console.log(`${lowerName} not found`);
    return;
  }
 
  let realQuantity = inventory[index].quantity;
  
  newQuantity = realQuantity - productQuantity;

}

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 {
      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);
```

it does not seem like you are actually changing inventory

you can also see that if you write removeProduct("FLOUR", 5); twice, you should see first 15 then 10 but that does not happen

I am not sure what to do to fix this. I have been trying for over a week now and I am getting frustrated.

I started all over and I somehow got the new code to work perfectly. Thank you for your help. :smiley: