Build an Inventory Management Program - Build an Inventory Management Program

Tell us what’s happening:

I cannot understand why the removeProduct test is not functioning yet it is functioning as described.

Your code so far

let inventory = [];
   
const findProductIndex = (productName) =>{
    return inventory.findIndex(product => product.name.toLowerCase() === productName.toLowerCase());
}

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

const removeProduct = (productName, quantity) =>{
    const productIndex = findProductIndex(productName.toLowerCase());
    if(productIndex === -1){
        console.log(`${productName.toLowerCase()} not found`);
    }else{
        let remainingQuantity = inventory[productIndex].quantity -= quantity;
        if(remainingQuantity === 0){
            inventory.splice(productIndex, 1);
            ;
        }else if(remainingQuantity < 0){
          inventory[productIndex].quantity += quantity;
          console.log(`Not enough ${productName.toLowerCase()} available, remaining pieces: ${inventory[productIndex].quantity}`)
        }
        else if(remainingQuantity > 0){
          console.log(`Remaining flour pieces: ${inventory[productIndex].quantity}`)
        }
    }
};

inventory = [{name: "flour", quantity: 20}, {name: "rice", quantity: 5}]
removeProduct("FLOUR", 12)






Your browser information:

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

Challenge Information:

Build an Inventory Management Program - Build an Inventory Management Program

Add this below your code, it represents all the things checked in the failing test, your removeProduct function has some issues:

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");