Build an Inventory Management Program - Build an Inventory Management Program

Tell us what’s happening:

Trying to figure it out: Heeellllppp!

addProduct({name: “FLOUR”, quantity: 5}) should push {name: “flour”, quantity: 5} to the inventory array when no object having name equal to “flour” is found in the inventory.
16. 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}].

Your code so far

let inventory = [];

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

function addProduct(productObj){
 let productIndex = findProductIndex(productObj.name);
 let productName = productObj.name.toLowerCase();
 
 if(productIndex !== -1){
   inventory[productIndex].quantity += productObj.quantity;
   console.log(`${productName} quantity updated`);
    
    }else{
      inventory.push({name: productName, quanity: productObj.quantity});
      console.log(`${productName} added to inventory`);

    }
  }

function removeProduct(productName, productQuantity){
  let productIndex = findProductIndex(productName);
  let normalizedName = productName.toLowerCase();

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

  }

  let item = inventory[productIndex];
  item.quantity -= productQuantity;

  if(item.quantity <= 0){
    console.log(`Not enough ${item.name} available, removing from inventory`);
    inventory.splice(productIndex, 1);
  }else{
    console.log(`Remaining ${item.name} pieces: ${item.quantity}`);
  }
} 
inventory.push({name: "Flour", quantity: 15});
inventory.push({name: "rice", quantity: 10});
inventory.push({name: "sugar", quantity: 5});

console.log




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 Edg/136.0.0.0

Challenge Information:

Build an Inventory Management Program - Build an Inventory Management Program

Hi @BooBoo212

When I view the inventory object I can see why flour is not found.

Happy cdoing

The issue lies within the removeProduct function, specifically with the if (item.quantity <= 0) block.

The problem is that the console.log message inside the if (item.quantity <= 0) condition is currently:
console.log(Not enough ${item.name} available, removing from inventory);

However, the exercise states that for removeProduct("FLOUR", 10) when the inventory is [{name: "flour", quantity: 5}, {name: "rice", quantity: 5}], it should log:
Not enough flour available, remaining pieces: 5

The current code’s console.log for item.quantity <= 0 does not match the expected output. It should be logging the remaining quantity, not a message about removing from inventory, when the quantity is reduced but not yet zero. The logic for logging “Remaining…” is in the else block, but the test case expects it even when item.quantity <= 0 but not yet fully removed.

The discrepancy is that the console.log message for the if (item.quantity <= 0) condition is not what the test case expects for the specific scenario where the quantity becomes negative but the item is still in the array before splice is called.