Tell us what’s happening:
I’ve tried everything but it fails on tests 12-16. When I check it in online compiler it renders same results needed
Your code so far
let inventory = [];
function findProductIndex(prodName) {
let chosenProd;
for (const prod of inventory) {
if (prod.name == prodName.toLowerCase()) {
chosenProd = prod;
};
};
return inventory.indexOf(chosenProd)
};
function addProduct(prodObj) {
let notFound = true;
for (let prod of inventory) {
if (prod.name.toLowerCase() == prodObj.name.toLowerCase()) {
notFound = false;
prod.quantity += prodObj.quantity
console.log(`${prod.name.toLowerCase()} quantity updated`)
break
};
};
if (notFound) {
let lowName = prodObj.name.toLowerCase()
let newObj = {
name: lowName,
quantity: prodObj.quantity
}
inventory.push(newObj);
console.log(`${newObj.name} added to inventory`)
}
};
//6
function removeProduct(prodName, quantity) {
let notFound = true;
let chosenProd;
for (prod of inventory) {
if (prod.name.toLowerCase() == prodName.toLowerCase()) {
notFound = false;
chosenProd = prod;
let result = chosenProd.quantity - quantity;
if (result == 0) {
let prodIndex = inventory.indexOf(chosenProd);
inventory.splice(prodIndex, 1)
} else if (result < 0) {
console.log(`Not enough ${chosenProd.name} available, remaining pieces: ${chosenProd.quantity}`)
} else if (result > 0) {
chosenProd.quantity = result;
console.log(`Remaining ${chosenProd.name} pieces: ${chosenProd.quantity}`)
}
};
};
if (notFound) {
console.log(`${prodName.toLowerCase()} not found`)
};
};
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Challenge Information:
Build an Inventory Management Program - Build an Inventory Management Program