Tell us what’s happening:
I have this done, all the test passed. Only 1 through me an error:
- removeProduct(“FLOUR”, 5) should log flour not found when no object having name equal to “flour” is found in the inventory array.
I cant understand why doesnt work!
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 removeProduct(productName, productQuantity){
let lowerName = productName.toLowerCase();
let index = findProductIndex(productName);
let realQuantity = inventory[index].quantity;
if(index === -1){
console.log(lowerName + " not found");
return;
}else if(index !== -1){
if(productQuantity === realQuantity){
inventory.splice(index, 1);
}else if(productQuantity > realQuantity){
console.log(`Not enough ${lowerName} available, remaining pieces: ${inventory[index].quantity}`);
}else if(productQuantity < realQuantity){
inventory[index].quantity -= productQuantity;
console.log(`Remaining ${lowerName} pieces: ${inventory[index].quantity}`)
}
}
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Challenge Information:
Build an Inventory Management Program - Build an Inventory Management Program