Build an Inventory Management Program - Build an Inventory Management Program

Tell us what’s happening:

My tests not passing, test 8, 9 and 10, 7th passed but rest not passing. I don’t understand what’s wrong. Please help me

Your code so far

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

const addProduct = productObject => {
   let productIndex = findProductIndex(productObject.name.toLowerCase());

   if(productIndex === -1) {
     inventory.push(productObject);
     console.log(productObject.name + " " + "added to inventory");
   }
   else {
     inventory[productIndex].quantity += productObject.quantity;
     console.log(productObject.name + " " + "quantity updated");
   }
}



Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36

Challenge Information:

Build an Inventory Management Program - Build an Inventory Management Program

Have you looked at the console when trying to use function as in tests?

Ie.:

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

Yes, console says - FLOUR added to inventory

I think I saw the problem now. When I again type - addProduct({name: "FLOUR", quantity: 5}) then it adds the FLOUR again but in a new object and also doesn’t update quantity.

Also name not converting to lower case but I written the code for it, please tell me how to fix it ?

you use toLowerCase in the first line of the function, but later when you use productObject again it is not the lowercase version of the name

2 Likes

I updated the code, 2 test passed, 1 still fails. I think I should use a variable for product.name.toLowerCase() instead of typing it everywhere. But on which 1 place I am missing it, please guide me.

Updated code-

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

const addProduct = productObject => {
   let productIndex = findProductIndex(productObject.name.toLowerCase());

   if(productIndex === -1) {
     inventory.push(productObject);
     console.log(productObject.name.toLowerCase() + " " + "added to inventory");
   }
   else {
     inventory[productIndex].quantity += productObject.quantity;
     console.log(productObject.name.toLowerCase() + " " + "quantity updated");
   }
}



Take another look at the output

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

Test 9 mentions in inventory should be {name: "flour", quantity: 5} object after calling addProduct({name: "FLOUR", quantity: 5}).

Yes, I saw that. But I don’t know where I am missing a .toLowerCase() method

Well, how the object passed to the addProduct function gets to the inventory list?

Got it :slight_smile: I will create a local variable inside function then use it instead.

I solved that but now in my removeProduct function, if statement not working

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

const addProduct = productObject => {
  productObject.name = productObject.name.toLowerCase();
   let productIndex = findProductIndex(productObject.name);

   if(productIndex === -1) {
     inventory.push(productObject);
     console.log(productObject.name + " " + "added to inventory");
   }
   else {
     inventory[productIndex].quantity += productObject.quantity;
     console.log(productObject.name + " " + "quantity updated");
   }
}


function removeProduct(productName, productQuantity) {
  productName = productName.toLowerCase();
  let index = findProductIndex(productName);
  let quantity = inventory[index].quantity - productQuantity;
  if(index === -1) {
    console.log(productName + " not found")
  } else if(index !== -1) {
    inventory[index].quantity -= productQuantity;
    console.log("Remaining " + productName + " pieces: " + quantity);
  } 
}

What do you mean not working? What exactly is not working?

if statement of my remove product function not passing test

Did you try troubleshooting it by sending some variables to console.log()?

log index right before the if. Put log statements after the if conditions and find out what the fail conditions are. Dig for more information.

What is index ? What is the value supposed to be?

What would cause index to be -1?

when no object with name property matching product name found then -1 returned.

Could you try to explain line-by-line, what’s happening in the function?

Can you find the line throwing this error?

TypeError: inventory[index] is undefined

removeProduct("FLOUR", 5)

Yes.

1st line - I make a function with two parameters, one for the name of product and another for quantity of product.

2nd line- I make sure that when function is executed, the product name should be in lower case only so I use a method.

3rd line - I get the index of the object of product inside using the 1st function I created.

4th line- I create a variable to find the present quantity - the quantity to be removed(argument one)

5th line- I check if the product specified is present or not, i not if not then index will be -1 because I made my 1st function like that.

6th line- I pass a string to console if above condition satisfied.

7th line- I check if product is present now. Like before based on 1st function.

8th line- I deduct the specified quantity as argument if product found in inventory.

9th line- I pass a statement to console based on above condition

yes I am getting an error but this -TypeError: Cannot read properties of undefined (reading ‘quantity’)