Build an Inventory Management Program - Build an Inventory Management Program

Tell us what’s happening:

My code is failing test 8,10,12,14,16. When I tried to test my code on the given conditions, it seemed to me the output is as required.
Could someone help me on this? Thanks very much!

Your code so far

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

const addProduct = (pobj) => {
  pobj.name = pobj.name.toLowerCase();
  let pind = findProductIndex(pobj.name);
  if (pind !== -1) {
    //update its quantity value
    inventory[pind].quantity += pobj.quantity;
    console.log(`${pobj.name} quantity updated`);
  } else {
    //push the product to the inventory array
    inventory.push(pobj);
    console.log(`${pobj.name} added to inventory`);
  }
}

const removeProduct = (pname, pquant) => {
  pname = pname.toLowerCase();
  let pind = findProductIndex(pname);
  if (pind !== -1) {
    if (inventory[pind].quantity > pquant) {
      inventory[pind].quantity -= pquant;
      console.log(`Remaining ${pname} pieces: ${inventory[pind].quantity}`)
    } else if (inventory[pind].quantity === pquant) { //remove product
      inventory.splice(pind, 1);
    } else { //not enough quantity to remove
      console.log(`Not enough ${pname} available, remaining pieces: ${inventory[pind].quantity}`);
    }
  } else { 
    //product not found
    console.log(`${pname} not found`);
  }

}


inventory.push({name: "flour", quantity: 5});
addProduct({name: "FLOUR", quantity: 5});
// inventory.push({name: "rice", quantity: 5})
// removeProduct("FLOUR", 5)
// console.log(inventory);
// addProduct({name: "FLOUR", quantity: 5});
// removeProduct("FLOUR", 20);
console.log(inventory);


Your browser information:

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

Challenge Information:

Build an Inventory Management Program - Build an Inventory Management Program

Your solution works from my end. Please try one of the following steps to move forward.

Click on the “Restart Step” button and force a refresh of your page with CTRL + F5 then try to paste the code in again.

or - Try the step in incognito or private mode.

or - Disable any/all extensions that interface with the freeCodeCamp website (such as Dark Mode, Ad Blockers, or Spellcheckers), and set your browser zoom level to 100%. Both of these factors can cause tests to fail erroneously.

or - Ensure your browser is up-to-date or try a different browser.

I hope one of these will work for you.

Thank you for your advice! It worked after I refresh the page and submit the same code again.