Tell us what’s happening:
Failing tests 12 and forward, but debugging lines all lead to the right output. I think I’ve got all the wording/spacing right, but must be missing something — can you help me find it?
Your code so far
let inventory = [];
function findProductIndex(product) {
if (inventory.length !== 0) {
for (let i = 0; i < inventory.length; i++) {
if (inventory[i].name.toLowerCase() === product.toLowerCase()) {
return i;
}
}
return -1;
}
return -1
}
function addProduct(newProduct) {
let index = findProductIndex(newProduct.name);
if (index === -1) {
newProduct.name = newProduct.name.toLowerCase();
inventory.push(newProduct);
console.log(`${inventory[inventory.length - 1].name} added to inventory`)
}
else {
inventory[index].quantity += newProduct.quantity;
console.log(`${inventory[index].name} quantity updated`)
};
}
function removeProduct(product) {
let index = findProductIndex(product.name)
if (index === -1) {
console.log(`${product.name.toLowerCase()} not found`)
}
else {
if (inventory[index].quantity > product.quantity) {
inventory[index].quantity -= product.quantity;
console.log(`Remaining ${product.name.toLowerCase()} pieces: ${inventory[index].quantity}`)
}
else {
if (inventory[index].quantity === product.quantity) {
inventory.splice(index, 1);
}
else {
console.log(`Not enough ${product.name.toLowerCase()} available, remaining pieces: ${inventory[index].quantity}`)
}
}
}
}
removeProduct({name: 'FLOUR', quantity: 5})
addProduct({name: "FLOUR", quantity: 17});
console.log(inventory)
addProduct({name: "flour", quantity: 0})
removeProduct({name: 'FLOUR', quantity: 4})
removeProduct({name: 'FLOUR', quantity: 5})
removeProduct({name: 'FLOUR', quantity: 8})
console.log(inventory)
addProduct({name: "FLOUR", quantity: 5});
addProduct({name: "rice", quantity: 5});
removeProduct({name: 'FLOUR', quantity: 10})
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.1 Safari/605.1.15 Ddg/26.1
Challenge Information:
Build an Inventory Management Program - Build an Inventory Management Program
###My output:
flour not found
flour added to inventory
[ { name: ‘flour’, quantity: 17 } ]
flour quantity updated
Remaining flour pieces: 13
Remaining flour pieces: 8
flour added to inventory
rice added to inventory
Not enough flour available, remaining pieces: 5