Build an Inventory Management Program - Build an Inventory Management Program

Tell us what’s happening:

Tests 13, 14 and 16 not passing relating to the removeProduct() function. Even though I’m getting the exact results mentioned in the pass conditions (logs the same result and inventory correctly updated). These are the conditions:
13. removeProduct(“FLOUR”, 5) should subtract 5 from the quantity value of the object having name “flour”
14. removeProduct(“FLOUR”, 5) should log Remaining flour pieces: 15 to the console
15. removeProduct(“FLOUR”, 10) should log Not enough flour available, remaining pieces: 5 when inventory is equal to [{name: “flour”, quantity: 5}, {name: “rice”, quantity: 5}].
Thank You and appreciate your help!!

Your code so far

const inventory = [];

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

const addProduct = (pObj) => {
  //check if product in inventory
  pObj.name = pObj.name.toLowerCase();
  let newProd = pObj.name;
  let newQuantity = pObj.quantity;
  for (let i = 0; i < inventory.length; i++) {
    if (newProd === inventory[i].name.toLowerCase()) {
      inventory[i].quantity += newQuantity;
      console.log(newProd + " " + "quantity updated");
      return;
    } 
  }
  inventory.push(pObj);
  console.log(pObj.name + " " + "added to inventory")
  return;
}

const removeProduct = (pName, pQuantity) => {
  pName = pName.toLowerCase();
  for (let i = 0; i < inventory.length; i++) {
    if (pName === inventory[i].name) {
        if ((inventory[i].quantity - pQuantity) > 0) {
        inventory[i].quantity = inventory[i].quantity - pQuantity;
        console.log(`Remaining ${pName} pieces: ${inventory[i].quantity}`);
        return;
        }else if ((inventory[i].quantity - pQuantity) === 0) {
            inventory.splice(i, 1);
            console.log("im here")
        } else {
          console.log(`Not enough ${pName} available, remaining pieces: ${inventory[i].quantity}`);
          return;
        }  
    } else {
      console.log(`${pName} not found`);
      return; 
    }
  } 
}




 
 

Your browser information:

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

Challenge Information:

Build an Inventory Management Program - Build an Inventory Management Program

add this below your code to test

inventory.length = 0;
inventory.push({name: 'flour', quantity: 15});
inventory.push({name: 'rice', quantity: 10});
inventory.push({name: 'sugar', quantity: 5});
removeProduct("flour", 10);
removeProduct("RICE", 5);
removeProduct("Sugar", 2);

it says “rice not found” and “sugar not found”

you may want to double check on your app logic

1 Like

I just looked through my loop logic and found the issue.
Thank you for the help!!