Build an Inventory Management Program - Build an Inventory Management Program

Tell us what’s happening:

I am having issues with the findProductIndex() function working properly. It works just fine when you call it and the array is empty. However, when it includes an element it still returns -1. For instance, I could use the following:

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

I ensure that the empty array contains an element before calling findProductIndex(), but it still returns -1 for some reason.

INITIAL PROBLEM | SOLVED

I am having issues parsing through an array of objects to determine if a property of an object such as name exists within the array. The issue I’m referring to can be found in the removeProduct() function towards the bottom where I added some debugging statements. I made an attempt with converting the array to a JSON string in order to make parsing more easier since its a string. However, when I use the .includes() method it doesn’t work as intended. I am unsure as to where to go from where since this is the only issue I’m having.

Your code so far

let inventory = [];

function findProductIndex(productName) {
  productName = productName.toLowerCase();

  for (const element of inventory) {
    if (element.name === productName) {
      return inventory(element);
    }
  }

  return -1;
}

function addProduct(productObject) {
  productObject.name = productObject.name.toLowerCase();

  for (const element of inventory) {
    if (productObject.name === element.name) {

      element.quantity += productObject.quantity;

      console.log(`${productObject.name} quantity updated`);

      break;
    } else {
      inventory.push(productObject);

      console.log(`${productObject.name} added to inventory`);

      break;
    }
  }
}

function removeProduct(productName, productQuantity) {
  productName = productName.toLowerCase();

  for (const element of inventory) {
    // REMOVE THIS WHEN DONE || DEBUGGING STATEMENT
    console.log("Product Name: " + element.name);

    if (element.name === productName) {
      element.quantity -= productQuantity;

      if (element.quantity === 0) {
        inventory.pop(element);

      } else if (element.quantity < 0) {
        console.log(`Not enough ${element.name} available, remaining pieces: ${element.quantity}`)
      }

      console.log(`Remaining ${element.name} pieces: ${element.quantity}`);
    }
  }

  console.log("\n#### DEBUGGING ####");
  const masterString = JSON.stringify(inventory);
  console.log("Inventory String: " + masterString);
  console.log("Product Name: " + productName);
  console.log("Does it exist in inventory? " + masterString.includes(productName));
  // console.log(`${productName} not found`); || MAIN ISSUE WITH FINDING THIS NAME IN AN ARRAY OF OBJECTS
}

// DEBUGGING
console.log("\n" + removeProduct("floUr", 1));
console.log("Current Inventory: " + inventory);

Console Output:

Product Name: flour
Remaining flour pieces: 4

#### DEBUGGING ####
Inventory String: [{"name":"flour","quantity":4}]
Product Name: flour
Does it exist in inventory? true

undefined
Current Inventory: [object Object]

The test cases don’t ask for any of the functions to have a return statement except findProductIndex() which is why undefined is seen in the console. Also, I just now realized that the console output for “Current Inventory: “ has changed. Initially, it printed out the array of elements, but now it says "[object Object]”. I don’t know why this had changed. I thought this may have been to due shallow copy by reference due to using JSON.stringify(), however I commented those debugging statements out and it still printed out as "[object Object]”.

EDIT:

I don’t know what happened, but I made a minor change and it works now. However, I still do not know why inventory is printing out as "[object Object]”.

Challenge Information:

Build an Inventory Management Program - Build an Inventory Management Program

IMPORTANT NOTE:

I am still having issues with the findProductIndex() function, as aforementioned but ran out of edits on the main post. It still returns -1 no matter what.

I just noticed a logical error in my program.

function addProduct(productObject) {

  productObject.name = productObject.name.toLowerCase();


  for (const element of inventory) {

    if (productObject.name === element.name) {


      element.quantity += productObject.quantity;


      console.log(`${productObject.name} quantity updated`);


      break;

    } else {

      inventory.push(productObject);


      console.log(`${productObject.name} added to inventory`);


      break;

    }

  }

}

This checks if the current element of the array has the property name that is equal to the given product name. If it’s not then it pushes that element to the array, but this is a flaw because the element it’s looking for could be the very next element in the array.

So, I made the following changes:

function addProduct(productObject) {

  productObject.name = productObject.name.toLowerCase();


  /* for (const element of inventory) {

    if (productObject.name === element.name) {


      element.quantity += productObject.quantity;


      console.log(`${productObject.name} quantity updated`);


      break;

    } else {

      inventory.push(productObject);


      console.log(`${productObject.name} added to inventory`);


      break;

    }

  } */


  const masterString = JSON.stringify(inventory);


  console.log("Checkpoint A");


  if (masterString.includes(productObject.name) === false) {

    console.log("Checkpoint B");


    inventory.push(productObject);

    console.log(`${productObject.name} added to inventory`);

  } else {

    console.log("Checkpoint C");


    for (const element of inventory) {

      if (productObject.name === element.name) {

        element.quantity += productObject.quantity;

        console.log(`${productObject.name} quantity updated`);

      }

    }

  }

}

This takes an inventory that has been turned into a JSON string and assigns it to masterString variable to see if this productObject currently exists.

However, I am still encountering a similar issue with TypeError: inventory is not a function being thrown in the console.

This is what I currently have being printed to the console:

// DEBUGGING

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

console.log("Index: " + findProductIndex("flour"));

console.log("Current Inventory: " + inventory);

Console Output:

Checkpoint A
Checkpoint B
flour added to inventory
undefined
TypeError: inventory is not a function

How can findProductIndex return anything other than -1 if there is nothing in inventory?

Once you add items to your inventory array, you should see an error in the console.

If your loop does find an element.name with productName, what is it actually returning? Is that an index? Is that the way to access an object in the inventory array?

In the future, please prepare your question on your local system before you post it. Eight edits and additional posts is excessive, and if you are changing your code as well after someone starts looking at it to help you, you are just wasting their time.

1 Like

Definitely was excessive on my end. I should’ve deleted the post and re-made a new one before anyone looked at it. I’ll do that going forward, as to not waste anybody’s time.

As for the first question, I realized I was using invalid logic or rather syntax when trying to access the index using a for…of loop. I altered that line of code to utilize .indexOf() instead and it seems to work just fine now. Furthermore, I call the addProduct() function first before calling the findProductIndex() as to add an object to the initially empty array. This allows me to then check if the findProductIndex() function is working as intended.

// DEBUGGING

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

console.log("Index: " + findProductIndex("flour"));

console.log("Current Inventory: " + inventory);

Though I am still having issues with the array being properly printed to the console. I do not know why it’s being printed out as [object Object] when it printed out the list of elements just fine before.

It should show up in your browser’s console.

1 Like

the console in the editor has limited capabilities, please open the browser console to see the full output

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.