Build an Inventory Management Program - Build an Inventory Management Program

Tell us what’s happening:

the code is working but i keep getting undefined after calling the function

Your code so far

let inventory = [];

function findProductIndex(productName) {
  // Convert the product name to lowercase for case-insensitive comparison
  const lowerCaseProductName = productName.toLowerCase();

  // Use findIndex to search the inventory array for a matching product
  return inventory.findIndex(
    (product) => product.name.toLowerCase() === lowerCaseProductName
  );
}

function addProduct(product) {
  const productIndex =  findProductIndex(product.name);

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

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

Your browser information:

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

Challenge Information:

Build an Inventory Management Program - Build an Inventory Management Program

Are you returning anything from your function?

no
the test no 9 keeps failing too

When it comes to console.log and functions, it will only print what the function returns.

And to pass test 10, you need to check your console output in addProduct. Do you know what those log statements are printing?

  1. Functions return undefined implicitly.
  1. console.log also return undefined after printing to the console. You do not see this in the fCC editor console, but you would in other runtimes, like the browser for example.