Tell us what’s happening:
My code is failing test 8,10,12,14,16. When I tried to test my code on the given conditions, it seemed to me the output is as required.
Could someone help me on this? Thanks very much!
Your code so far
let inventory = [];
const findProductIndex = (pname) => {
pname = pname.toLowerCase();
for (let i=0; i<inventory.length; i++) {
if (inventory[i].name === pname) {
return i;
}
}
return -1;
}
const addProduct = (pobj) => {
pobj.name = pobj.name.toLowerCase();
let pind = findProductIndex(pobj.name);
if (pind !== -1) {
//update its quantity value
inventory[pind].quantity += pobj.quantity;
console.log(`${pobj.name} quantity updated`);
} else {
//push the product to the inventory array
inventory.push(pobj);
console.log(`${pobj.name} added to inventory`);
}
}
const removeProduct = (pname, pquant) => {
pname = pname.toLowerCase();
let pind = findProductIndex(pname);
if (pind !== -1) {
if (inventory[pind].quantity > pquant) {
inventory[pind].quantity -= pquant;
console.log(`Remaining ${pname} pieces: ${inventory[pind].quantity}`)
} else if (inventory[pind].quantity === pquant) { //remove product
inventory.splice(pind, 1);
} else { //not enough quantity to remove
console.log(`Not enough ${pname} available, remaining pieces: ${inventory[pind].quantity}`);
}
} else {
//product not found
console.log(`${pname} not found`);
}
}
inventory.push({name: "flour", quantity: 5});
addProduct({name: "FLOUR", quantity: 5});
// inventory.push({name: "rice", quantity: 5})
// removeProduct("FLOUR", 5)
// console.log(inventory);
// addProduct({name: "FLOUR", quantity: 5});
// removeProduct("FLOUR", 20);
console.log(inventory);
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 Edg/138.0.0.0
Challenge Information:
Build an Inventory Management Program - Build an Inventory Management Program