Build an Inventory Management Program - Build an Inventory Management Program

Tell us what’s happening:

Hello! Steps 12 and 14 - 16 seem to not be passing for me (ie. the “removeProduct” function), even though I’m able to get the correct outputs within the console. I’ve added in some test code. Can someone please help me understand why I’m able to get the right outputs, but the lab isn’t passing me?

I’m wondering if I’m misunderstanding some of the requirements or test cases, perhaps. Thank you!

Your code so far


function removeProduct (productName, productQuantity) {
  let lowerCaseProductName = productName.toLowerCase();
// Test code starts
  console.log(inventory);
  console.log(`The amount of ${lowerCaseProductName} being removed is ${productQuantity}`);
// Test code ends
  for (let item of inventory) {
    if (lowerCaseProductName === item.name) {
      item.quantity -= productQuantity;
      if (item.quantity === 0) {
        inventory.splice(1, inventory.indexOf(item));
        return;
      } else if (item.quantity > 0) {
        return console.log(`Remaining ${item.name} pieces: ${item.quantity}`);
      } else if (item.quantity < 0) {
        item.quantity += productQuantity;
        return console.log(`Not enough ${item.name} available, remaining pieces: ${item.quantity}`)
      } 
    }
  }
  return console.log(`${lowerCaseProductName} not found`);
}

removeProduct("FLOUR", 5)
removeProduct("FLOUR", 30)
removeProduct("FLOUR", 15)
removeProduct("FLOUR3", 6)

Your browser information:

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

Challenge Information:

Build an Inventory Management Program - Build an Inventory Management Program

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-inventory-management-program/66d75dd0aa65a71600dc669b.md at main · freeCodeCamp/freeCodeCamp · GitHub

Hi @snaylspace,

I’m seeing this error in the console:
ReferenceError: inventory is not defined

Please post your complete code.

Happy coding

Hi @dhess - I cut out some of the other code I wrote which was used to solve earlier test cases in the lab. That might be why you are getting that output. Here’s the full code body which for me isn’t throwing that error:

let inventory = [];

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

function findProductIndex (productName) {
  let lowerCaseProductName = productName.toLowerCase();
  for (let item of inventory) {
    if (item.name === lowerCaseProductName) {
      return inventory.indexOf(item);
    }
  }
  return -1;
}

console.log(findProductIndex("flour"));
console.log(findProductIndex("FloUr"));

function addProduct (productObject) {
  let lowerCaseProductName = productObject.name.toLowerCase();
  for (let item of inventory) {
    if (lowerCaseProductName === item.name) {
      item.quantity += productObject.quantity;
      return console.log(`${lowerCaseProductName} quantity updated`);
    } 
  }
  inventory.push({name: lowerCaseProductName, quantity: productObject.quantity});
  return console.log(`${lowerCaseProductName} added to inventory`);
}

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

function removeProduct (productName, productQuantity) {
  let lowerCaseProductName = productName.toLowerCase();
  // Test code starts
  console.log(inventory);
  console.log(`The amount of ${lowerCaseProductName} being removed is ${productQuantity}`);
  // Test code ends
  for (let item of inventory) {
    if (lowerCaseProductName === item.name) {
      item.quantity -= productQuantity;
      if (item.quantity === 0) {
        inventory.splice(1, inventory.indexOf(item));
        return;
      } else if (item.quantity > 0) {
        return console.log(`Remaining ${item.name} pieces: ${item.quantity}`);
      } else if (item.quantity < 0) {
        item.quantity += productQuantity;
        return console.log(`Not enough ${item.name} available, remaining pieces: ${item.quantity}`)
      } 
    }
  }
  return console.log(`${lowerCaseProductName} not found`);
}

removeProduct("FLOUR", 5)
removeProduct("FLOUR", 30)
removeProduct("FLOUR", 15)
removeProduct("FLOUR3", 6)

When you use this, are you still seeing that message?

Thank you for posting all of your code.

This syntax is incorrect. Look it up, please.

You are asked to log messages, not to return messages.

The use of return and console.log() are mutually exclusive.

Note: To prevent conflicts, keep only the logging mentioned in the user stories when running tests.

Also, please note the above instruction.

Happy coding

Thanks @dhess - Very helpful. Was able to pass.

I checked the GitHub solution suggestions as well and noticed that another way of approaching this problem is by creating a productIndex variable.

Is that a preferred or more elegant solution because it takes up say less processing or computer power/memory/time? I’m not sure how that part of coding works yet…

You have already created a findProductIndex function to look for the product in inventory so, yes, it’s more efficient to use that rather than repeating the same code that’s inside that function…often known as not “recreating the wheel.”