Hello! Steps 12 and 14 - 16 seem to not be passing for me (ie. the “removeProduct” function), even though I’m able to get the correct outputs within the console. I’ve added in some test code. Can someone please help me understand why I’m able to get the right outputs, but the lab isn’t passing me?
I’m wondering if I’m misunderstanding some of the requirements or test cases, perhaps. Thank you!
Your code so far
function removeProduct (productName, productQuantity) {
let lowerCaseProductName = productName.toLowerCase();
// Test code starts
console.log(inventory);
console.log(`The amount of ${lowerCaseProductName} being removed is ${productQuantity}`);
// Test code ends
for (let item of inventory) {
if (lowerCaseProductName === item.name) {
item.quantity -= productQuantity;
if (item.quantity === 0) {
inventory.splice(1, inventory.indexOf(item));
return;
} else if (item.quantity > 0) {
return console.log(`Remaining ${item.name} pieces: ${item.quantity}`);
} else if (item.quantity < 0) {
item.quantity += productQuantity;
return console.log(`Not enough ${item.name} available, remaining pieces: ${item.quantity}`)
}
}
}
return console.log(`${lowerCaseProductName} not found`);
}
removeProduct("FLOUR", 5)
removeProduct("FLOUR", 30)
removeProduct("FLOUR", 15)
removeProduct("FLOUR3", 6)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36
Challenge Information:
Build an Inventory Management Program - Build an Inventory Management Program
Hi @dhess - I cut out some of the other code I wrote which was used to solve earlier test cases in the lab. That might be why you are getting that output. Here’s the full code body which for me isn’t throwing that error:
let inventory = [];
inventory = [
{name: "bread", quantity: 20},
{name: "rice", quantity: 5},
{name: "flour", quantity: 15}
];
function findProductIndex (productName) {
let lowerCaseProductName = productName.toLowerCase();
for (let item of inventory) {
if (item.name === lowerCaseProductName) {
return inventory.indexOf(item);
}
}
return -1;
}
console.log(findProductIndex("flour"));
console.log(findProductIndex("FloUr"));
function addProduct (productObject) {
let lowerCaseProductName = productObject.name.toLowerCase();
for (let item of inventory) {
if (lowerCaseProductName === item.name) {
item.quantity += productObject.quantity;
return console.log(`${lowerCaseProductName} quantity updated`);
}
}
inventory.push({name: lowerCaseProductName, quantity: productObject.quantity});
return console.log(`${lowerCaseProductName} added to inventory`);
}
addProduct({name: "FLOUR", quantity: 5});
addProduct({name: "FLOUR2", quantity: 5});
function removeProduct (productName, productQuantity) {
let lowerCaseProductName = productName.toLowerCase();
// Test code starts
console.log(inventory);
console.log(`The amount of ${lowerCaseProductName} being removed is ${productQuantity}`);
// Test code ends
for (let item of inventory) {
if (lowerCaseProductName === item.name) {
item.quantity -= productQuantity;
if (item.quantity === 0) {
inventory.splice(1, inventory.indexOf(item));
return;
} else if (item.quantity > 0) {
return console.log(`Remaining ${item.name} pieces: ${item.quantity}`);
} else if (item.quantity < 0) {
item.quantity += productQuantity;
return console.log(`Not enough ${item.name} available, remaining pieces: ${item.quantity}`)
}
}
}
return console.log(`${lowerCaseProductName} not found`);
}
removeProduct("FLOUR", 5)
removeProduct("FLOUR", 30)
removeProduct("FLOUR", 15)
removeProduct("FLOUR3", 6)
When you use this, are you still seeing that message?
I checked the GitHub solution suggestions as well and noticed that another way of approaching this problem is by creating a productIndex variable.
Is that a preferred or more elegant solution because it takes up say less processing or computer power/memory/time? I’m not sure how that part of coding works yet…
You have already created a findProductIndex function to look for the product in inventory so, yes, it’s more efficient to use that rather than repeating the same code that’s inside that function…often known as not “recreating the wheel.”