Build an Inventory Management Program

Tell us what’s happening:

In Build an Inventory Management Program,

My code could not pass test 3,4,5,7,8,9,10,12,13,14,15,16

(ony 1, 2, 6, 11 passed, which means all exactly work tests failed)

I saw my test works well, I don’t konw what’s wrong.

Your code so far

let inventory = [];

/*
// test data
inventory = [
    { name: "apple", quantity: 50 },
    { name: "banana", quantity: 30 },
    { name: "flour", quantity: 20 }
];
*/

function findProductIndex(productName) {
    for (product of inventory) {
        if (product.name === productName.toLowerCase()) {
            return inventory.indexOf(product);
        }
    }
    return -1;
}

function addProduct(new_product) {
    let index = findProductIndex(new_product.name);

    if (index !== -1) {
        inventory[index].quantity += new_product.quantity;
        console.log(`${inventory[index].name} quantity updated`);
    } else {
        let productToAdd = { ...new_product, name: new_product.name.toLowerCase() };
        inventory.push(productToAdd);
        console.log(`${productToAdd.name} added to inventory`);
    }
}

function removeProduct(productName, count) {
    let index = findProductIndex(productName);
    if (index === -1) {
        console.log(`${productName} not found`);
    }
    else {
        if (inventory[index].quantity < count) {
            console.log(`Not enough ${inventory[index].name} available, remaining pieces: ${inventory[index].quantity}.`);
        }
        else if (inventory[index].quantity === count) {
            inventory.splice(index, 1);
        }
        else {
            inventory[index].quantity -= count;
            console.log(`Remaining ${inventory[index].name} pieces: ${inventory[index].quantity}`);
        }
    }
}

/*
// Test cases
console.log(inventory);                         // show initial inventory
console.log(findProductIndex("flour"));         // test findProductIndex for existing product
console.log(findProductIndex("FloUr"));         // test findProductIndex for case insensitivity
console.log(findProductIndex("sugar"));         // test findProductIndex for non-existing product
addProduct({ name: "FLOUR", quantity: 8 });     // test addProduct for existing product
console.log(inventory);
addProduct({ name: "RICE", quantity: 25 });     // test addProduct for new product
console.log(inventory);
removeProduct("FLOUR", 3);                      // test removeProduct for existing product with sufficient quantity
console.log(inventory);
removeProduct("FLOUR", 30);                     // test removeProduct for existing product with insufficient quantity
console.log(inventory);
removeProduct("RICE", 25);                      // test removeProduct for existing product with exact quantity
console.log(inventory);
removeProduct("SUGAR", 5);                      // test removeProduct for non-existing product
*/

Challenge Information:

Build an Inventory Management Program - Build an Inventory Management Program

If I uncomment your test data and console.log(findProductIndex("FloUr")); , I see this error in the console:

ReferenceError: product is not defined

Really?
I did as you said, make code looks like:

let inventory = [];


// test data
inventory = [
    { name: "apple", quantity: 50 },
    { name: "banana", quantity: 30 },
    { name: "flour", quantity: 20 }
];


function findProductIndex(productName) {
    ...
}

function addProduct(new_product) {
    ...
}

function removeProduct(productName, count) {
    ...
}

/*
// Test cases
    ...
*/
console.log(findProductIndex("FloUr")); 

I run it, and I get:

PS E:\Code\JavaScript> node '.\Inventory Management Program.js'
2

Which is the exactly expected result.
I didn’t see that error you show.

Wait, I got it.

In function findProductIndex()

function findProductIndex(productName) {
    for (product of inventory) {
        if (product.name === productName.toLowerCase()) {
            return inventory.indexOf(product);
        }
    }
    return -1;
}

product is not defined

It is a mistake, but some how it runs normally, no error or warning, no wrong output.
Maybe in my runtime environment, error checking is not that strict, or there’s some kind of self-correction?

After fixxing this, and some other output format mistake, all tests passed

Thanks for you help!

there is a thing called strict mode, you may want to read about it: Strict mode - JavaScript | MDN