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