Tell us what’s happening:
I think I’m missing some minor thing that is driving me nuts. Test 8, 10, 12, 14 and 16 keep failing even though the output is exactly what is expected. I have spent hours over the last few days trying different things but haven’t narrowed the failures down further.
Your code so far
const inventory = [];
const findProductIndex = (prodName) => {
const prodNameLower = prodName.toLowerCase();
let found = -1
for(let i= 0; i < inventory.length; i++){
if(prodNameLower === inventory[i].name){
found = i;
}
}
return found;
}
const addProduct = (prodObj) => {
const index = findProductIndex(prodObj.name.toLowerCase());
//confirm exists
if(index !== -1){
inventory[index].quantity += prodObj.quantity;
return `${prodObj.name.toLowerCase()} quantity updated`;
}
else {
inventory.push({name: prodObj.name.toLowerCase(), quantity: prodObj.quantity});
return `${prodObj.name.toLowerCase()} added to inventory`;
}
}
const removeProduct = (name, quantity) => {
const index = findProductIndex(name.toLowerCase());
//confirm exists
if(index !== -1){
//check removed is not greater than quantity
if(inventory[index].quantity < quantity){
return `Not enough ${name.toLowerCase()} available, remaining pieces: ${inventory[index].quantity}`;
}
else{
//check amount left before updating inventory
const left = inventory[index].quantity - quantity;
if(left === 0){
inventory.splice(index, 1);
}
if(left > 0){
inventory[index].quantity -= quantity;
return `Remaining ${name.toLowerCase()} pieces: ${inventory[index].quantity}`
}
}
}
else {
return `${name.toLowerCase()} not found`
}
}
console.log(removeProduct("FLOUR", 5)) //testing #12
console.log(addProduct({name: "FLOUR", quantity: 5})) //testing #10
addProduct({name: "Rice", quantity: 5}) //added for testing
console.log(inventory) //show inventory prior to test #16
console.log(removeProduct("FLOUR", 10)) //testing #16
console.log(addProduct({name: "FLOUR", quantity: 5})) //testing #8
addProduct({name: "FLOUR", quantity: 10}) //added for testing
console.log(inventory)//show inventory prior to test #14
console.log(removeProduct("FLOUR", 5)) //testing #14
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36
Challenge Information:
Build an Inventory Management Program - Build an Inventory Management Program