I am still stuck on Step 13 not passing. I am at my wit’s end. Please help me figure out where I went wrong. I keep getting the message “removeProduct(“FLOUR”, 5) should subtract from the quantity value of the object having name equal to “flour” inside the inventory array.” Where am I going wrong? Thanks!
Your code so far
let inventory = []
function findProductIndex(productName){
let index = inventory.findIndex(obj => obj.name === productName.toLowerCase());
return index;
}
function addProduct(productObject){
let newObjectLowerCase = productObject.name.toLowerCase();
let index = inventory.findIndex(item => item.name === newObjectLowerCase);
if(index !== -1){
inventory[index].quantity += productObject.quantity;
console.log(`${newObjectLowerCase} quantity updated`);
} else {
inventory.push({name:newObjectLowerCase, quantity:productObject.quantity});
console.log(newObjectLowerCase + " added to inventory");
}
}
function removeProductOLD(productName, productQuantity) {
let lowerName = productName.toLowerCase();
let index = findProductIndex(lowerName);
if (index === -1) {
console.log(`${lowerName} not found`);
return;
}
let realQuantity = inventory[index].quantity;
newQuantity = realQuantity - productQuantity;
}
function removeProduct(productName, productQuantity) {
let lowerName = productName.toLowerCase();
let index = findProductIndex(lowerName);
if (index === -1) {
console.log(`${lowerName} not found`);
return;
}
let realQuantity = inventory[index].quantity;
if (productQuantity === realQuantity) {
inventory.splice(index, 1);
} else if (productQuantity > realQuantity) {
console.log(`Not enough ${lowerName} available, remaining pieces: ${realQuantity}`);
} else {
/* inventory[index].quantity -= productQuantity; */
productQuantity = realQuantity - productQuantity;
console.log(`Remaining ${lowerName} pieces: ${productQuantity}`);
}
}
inventory.push({name: "flour", quantity: 20});
inventory.push({name: "first", quantity: 10});
inventory.push({name: "second", quantity: 20});
inventory.push({name: "third", quantity: 30});
removeProduct("FLOUR", 5);
removeProduct("First", 5);
removeProduct("SEcond", 5);
removeProduct("THIrd", 5);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/150.0.0.0 Safari/537.36 Edg/150.0.0.0
Challenge Information:
Build an Inventory Management Program - Build an Inventory Management Program
I am still getting the step 13 error message “removeProduct("FLOUR", 5) should subtract 5 from the quantity value of the object having name equal to "flour" inside the inventory array.” I just don’t understand this.