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