Tell us what’s happening:
My code does not pass test 16 from “Build an Inventory Management Program”. I don’t know where to look for an error as the output is correct.
Your code so far
type or paste code here
let inventory = []; // {"name": "", "quantity": 0}
function findProductIndex(productName) {
let productNameLowerCase = productName.toLowerCase();
let productIndex = -1;
for (let index in inventory) {
if (inventory[index].name === productNameLowerCase) {
productIndex = index;
}
}
return Number(productIndex);
}
function addProduct(productObj) {
let productIndex = findProductIndex(productObj.name);
let newProductObj = {
name: productObj.name.toLowerCase(),
quantity: productObj.quantity
}
for (let i = 0; i < inventory.length; i++) {
if (inventory[i].name == productObj.name.toLowerCase()) {
inventory[i].quantity += productObj.quantity;
console.log(productObj.name.toLowerCase() + " quantity updated");
}
}
if (productIndex === -1) {
inventory.push(newProductObj);
console.log(newProductObj.name + " added to inventory");
}
}
function removeProduct(productName, productQuantity) {
let newInventory = [];
let productIndex = findProductIndex(productName);
let currentIndex;
if (productIndex === -1) {
console.log(productName.toLowerCase() + " not found");
}
for (let i = 0; i < inventory.length; i++) {
let remainingQuantitiy = productQuantity - inventory[i].quantity;
if (inventory[i].name == productName.toLowerCase()) {
inventory[i].quantity -= productQuantity;
if (inventory[i].quantity <= 0) {
currentIndex = i;
console.log(`Not enough ${inventory[i].name} available, remaining pieces: ${remainingQuantitiy}`);
}
else { console.log(`Remaining ${inventory[i].name} pieces: ${inventory[i].quantity}`);
}
}
}
for (let itemIndex = 0; itemIndex < inventory.length; itemIndex++) {
if (itemIndex != currentIndex) {
newInventory.push(inventory[itemIndex]);
}
}
inventory = newInventory;
}
inventory.length = 0;
inventory.push({name: 'flour', quantity: 15});
inventory.push({name: 'rice', quantity: 10});
inventory.push({name: 'sugar', quantity: 5});
console.log("actual:")
removeProduct("Flour", 5);
console.log("expected:")
console.log( "Remaining flour pieces: 10");
console.log("\nactual:")
removeProduct("RICE", 5);
console.log("expected:")
console.log( "Remaining rice pieces: 5");
console.log("\nactual:")
removeProduct("Sugar", 2);
console.log("expected:")
console.log("Remaining sugar pieces: 3");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:136.0) Gecko/20100101 Firefox/136.0
Challenge Information:
Build an Inventory Management Program - Build an Inventory Management Program