Build an Inventory Management Program

You can look at the project for context.

//Global
  //Variables
    let inventory = [{name: "banana", quantity: 4}, {name: "chin", quantity: 6}, {name: "yelb", quantity: 10}];

  //Functions
    function findProductIndex (item) {
      item = item.toLowerCase();
      for (let i = 0; i < inventory.length; i++) {
        if (inventory[i].name == item) {
          return i;
        } else if (i == inventory.length - 1) {
          return -1;
        }
      }
    }

    function addProduct (product) {
      if (findProductIndex(product.name) == -1) {
        inventory.push(product);
        console.log(`${product.name} added to inventory`);
      } else {
        inventory[findProductIndex(product.name)].quantity += product.quantity;
        console.log(`${product.name} quantity updated`);
      }
    }
    
    function removeProduct (name, quantity) {
      if (findProductIndex(name) == -1) {
        console.log(`${name} not found`);
      } else if (quantity > inventory[findProductIndex(name)].quantity) {
        console.log(`Not enough ${name} available, remaining pieces: ${inventory[findProductIndex(name)].quantity}`);
      } else {
        inventory[findProductIndex(name)].quantity -= quantity;
        console.log(`Remaining ${name} pieces: ${inventory[findProductIndex(name)].quantity}`);
        if (inventory[findProductIndex(name)].quantity == 0) {
          inventory.splice(inventory[findProductIndex(name)], 1);
          console.log("This is running");
        }
      }
    }

I have tried to use the splice function to remove the product in the removeProduct function to remove products but when I call it, it removes the first element in the array, banana.

Here is how I am calling it and checking the array:

removeProduct("yelb", 10);
console.log(inventory);

hello! please consider what inventory.splice() is expecting as arguments. :))

JavaScript Array splice()

It expects the index (which I think I’m getting with find product index), and the number of items to delete.

are you sure you are passing the index?

1 Like

Thank you that fixed that problem.

Here, in the else statement, I thought I was adding the input quantity to the quantity property, but in the console it says TypeError: Cannot read properties of undefined (reading 'quantity') and I haven’t found where that is originating from.

Update: I have found where it is coming from, it’s the line in which I try to add the new quantity to the old.

I’m not sure why it’s giving me that error though because I don’t see any reason for it not to work.
Here is how I’m trying to call the function:

addProduct({name: "FLOUR", quantity: 5});

Actually there’s nothing wrong with that.
Test 8 is failing even though when I run that code it does what the test want me to do.

Sorry about posting so often, I will try to debug more before I make a post or reply to something.

does your code fulfill this user story completely? what doesfindProductIndex() return if you call it when inventory is empty?

  1. You should create a function named findProductIndex that takes the product name as its argument and returns the index of the corresponding product object inside the inventory array. The function should always use the lowercase form of the product name to perform the search. If the product is not found, the function should return -1.