Build an Inventory Management Program - Build an Inventory Management Program

Tell us what’s happening:

My function is very much getting the correct things logged and doing the correct maths, why is test 13, 14, and 16 failing?

Your code so far

let inventory = [{name: "flour", quantity: 20}, {name: "rice", quantity: 5}];
function findProductIndex(proName){
  for (let i = 0; i < inventory.length; i++){
    if (proName.toLowerCase() == inventory[i].name){
      return i
    }
  }
  return -1
}

//Good 2 here

const addProduct = (object) => {
  let productName = object.name.toLowerCase();
  object.name = object.name.toLowerCase();
  if (findProductIndex(object.name) == -1) {
    inventory.push(object);
    console.log(`${productName} added to inventory`)
  }
  else {
    let added = object.quantity
    for (let i = 0; i < inventory.length; i++){
      if (productName == inventory[i].name){
        inventory[i].quantity += added;
        console.log(`${inventory[i].name} quantity updated`)
      }
    }
  }
};

//Good to Here

const removeProduct = (productName, productQuantity) => {
  let index = findProductIndex(productName);
  productName = productName.toLowerCase();
  if (index == -1){
    console.log(`${productName} not found`)
  }
  else {
    for (let i = 0; i < inventory.length; i++) {
      if ((inventory[i].quantity - productQuantity) < 0){
        console.log(`Not enough ${productName} available, remaining pieces: ${inventory[i].quantity}`)
      }
      else if ((inventory[i].quantity - productQuantity) == 0){
        inventory.splice(index, 1);
      }
      else if ((inventory[i].quantity - productQuantity) > 0){
        inventory[i].quantity -= productQuantity;
         console.log(`Remaining ${productName} pieces: ${inventory[i].quantity}`)
      }
    }
  }
}


console.log(removeProduct("FLOUR", 5))

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36

Challenge Information:

Build an Inventory Management Program - Build an Inventory Management Program

Hi @sagem0530

You should declare an empty array named inventory.

You need to start with an empty array for the inventory variable.

Then try adding a product to the inventory.

Happy coding

Hi @sagem0530,

What do you see in the console when you test like this:

let inventory = [{name: "rice", quantity: 5}, {name: "flour", quantity: 20}];
console.log(removeProduct("FLOUR", 20));
console.log(inventory);

Happy coding!

Okay so I added a “break” command to them and that seemed to fix 14 but not 13/16, I see that it was just re-looping each time which makes sense but now I don’t see why it’s still not working

inventory = [{name: "flour", quantity: 5}, {name: "rice", quantity: 5}];


const removeProduct = (productName, productQuantity) => {

let index = findProductIndex(productName);

  productName = productName.toLowerCase();

if (index == -1){

    console.log(`${productName} not found`)

}

else {

for (let i = 0; i < inventory.length; i++) {

if ((inventory[i].quantity - productQuantity) < 0){

        console.log(`Not enough ${productName} available, remaining pieces: ${inventory[i].quantity}`);

break

}

else if ((inventory[i].quantity - productQuantity) == 0){

        inventory.splice(index, 1);

}

else {

        inventory[i].quantity -= productQuantity;

         console.log(`Remaining ${productName} pieces: ${inventory[i].quantity}`);

break

}

}

}

}

Why do you think you need to loop? Don’t you already have the index where the product object lives in the inventory array?


I sure don’t need to loop! I don’t why I thought that was necessary actually! Passed it immediately! Thank you!