Tell us what’s happening:
It looks like the program is doing what is should but I fail the test for the addProduct snippet.
Checked multiple posts and forums but cannot find what’s wrong.
Your code so far
let inventory = [];
//Find the product index
function findProductIndex(name) {
let productName = name.toLowerCase();
for (let i = 0; i < inventory.length; i++) {
if (inventory[i].name === productName) {
return i;
}
}
return -1;
}
//Add product or change quantity
function addProduct(name, quantity) {
let productName = name.toLowerCase();
let index = findProductIndex(productName);
if (index !== -1) {
inventory[index].quantity += quantity;
console.log(`${inventory[index].name} quantity updated`);
} else {
inventory.push({ name: productName, quantity });
console.log(`${productName} added to inventory`);
}
}
console.log(inventory);
console.log(findProductIndex("FLOUR"));
addProduct("FLOUR", 5);
console.log(findProductIndex("flOUr"));
addProduct("RiCe", 5);
addProduct("FLOUR", 5)
console.log(inventory);
//Remove product or reduce quantity
function removeProduct(name, quantity) {
let productName = name.toLowerCase();
let index = findProductIndex(productName);
if (index === -1) {
console.log(`${productName} not found`);
return;
}
if (inventory[index].quantity > quantity) {
inventory[index].quantity -= quantity;
console.log(`Remaining ${productName} pieces: ${inventory[index].quantity}`);
}
else if (inventory[index].quantity === quantity) {
inventory.splice(index, 1);
}
else {
console.log(`Not enough ${productName} available, remaining pieces: ${inventory[index].quantity}`)
}
}
removeProduct("eggs", 15);
removeProduct("FLOUR", 3);
removeProduct("FLour", 8);
console.log(inventory);
removeProduct("FLour", 7);
console.log(inventory);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36
Challenge Information:
Build an Inventory Management Program - Build an Inventory Management Program