Build an Inventory Management Program - Build an Inventory Management Program

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

can you explain the logic you followed in your own words and see if your code does that correctly as the lab requires ?

Like, if you have flour of quantity 15 and you try to remove 20, as this can’t be done, you should log the message of unavailable quantity with remaining of 15.
(the quantity you have in your inventory, not the subtraction result)

also, why would I iterate all over the array to find the correct product object I’m looking for if I already have it from my findProductIndex() function stored in the productIndex variable.

and after I get it, I do inventory[i].quantity -= productQuantity without checking if that will cause a negative result or not.

Overall, your logic is almost correct but it needs some changes if you try to understand the challenge better and try to solve it with clear steps before implementing it in code.

check this out:

inventory.length = 0;
inventory.push({name: 'flour', quantity: 15});
inventory.push({name: 'rice', quantity: 10});
inventory.push({name: 'sugar', quantity: 5});
removeProduct("flour", 20);
removeProduct("RICE", 20);
removeProduct("Sugar", 20);

it prints

Not enough flour available, remaining pieces: 5
Not enough rice available, remaining pieces: 10
Not enough sugar available, remaining pieces: 15

aren’t the remaining pieces wrong?

I have to do/thinks this again. I’ll come back to this later.