Build an Inventory Management Program - Build an Inventory Management Program

Tell us what’s happening:

unable to get trhu to step 15 even though all work fine

Your code so far

let inventory=[];

function findProductIndex(proName)
{
  for(let i=0; i<inventory.length; i++)
  {
   if(proName.toLowerCase()==inventory[i].name.toLowerCase())
     {
      return i;
      }
   
}
return -1;
}
//console.log(findProductIndex("flour"));

function addProduct(productObject)
 {
   productObject.name=productObject.name.toLowerCase();
   let productIndex = findProductIndex(productObject.name);
   

   if(productIndex === -1) {
     inventory.push(productObject);
     console.log(productObject.name + " " + "added to inventory");
   }
   else {
     inventory[productIndex].quantity += productObject.quantity;
     console.log(productObject.name + " " + "quantity updated");
   }
}
 //addProduct({name: "FLOUR", quantity: 5});
 //addProduct({name: "FLOUR", quantity: 5});




function removeProduct(productName,proQuantity)
 {
        productName=productName.toLowerCase();

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

   if(inventory[productIndex].quantity>=proQuantity)
   {
  inventory[productIndex].quantity -= proQuantity;
   console.log(`Remaining ${productName} pieces: ${inventory[productIndex].quantity}`);
   }
   
 else if(inventory[productIndex].quantity<=0)
 {
   console.log("Item removed");
     inventory.splice(inventory[productIndex]); 
   
 }
 //else if(inventory[productIndex].quantity==0)
 else
   {
    console.log(`Not enough ${productName} available, remaining pieces: ${inventory[productIndex].quantity}`);
   }
 return;
 }

 /*addProduct({name: "FLOUR", quantity: 5})

addProduct({name: "FLOUR", quantity: 5})
addProduct({name: "FLOUR", quantity: 5})
addProduct({name: "FLOUR", quantity: 5})

console.log(inventory);
removeProduct("FLOUR", 5)
removeProduct("FLOUR", 5)
 removeProduct("FLOUR", 10)
 removeProduct("FLOUR", 5);
 console.log(inventory);
 //addProduct({name: "FLOUR", quantity: 5});
 removeProduct("FLOUR", 5);
  console.log(inventory);
   removeProduct("FLOUR", 5);
      removeProduct("FLOUR", 5);*/
          

inventory.length = 0;
inventory.push({name: 'flour', quantity: 15});
inventory.push({name: 'rice', quantity: 10});
inventory.push({name: 'sugar', quantity: 5});
console.log("actual:")
removeProduct("Flour", 5);
console.log("expected:")
console.log( "Remaining flour pieces: 10");
console.log("\nactual:");
removeProduct("RICE", 5);
console.log("expected:")
console.log( "Remaining rice pieces: 5");
console.log("\nactual:")
removeProduct("Sugar", 2);
console.log("expected:");
console.log("Remaining sugar pieces: 3");






 

  

Your browser information:

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

Challenge Information:

Build an Inventory Management Program - Build an Inventory Management Program

try to remove all pieces of a certain item
add console.log(inventory)
make sure you don’t have quantity: 0 remaining but that the item is removed completely

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.