Build an Inventory Management Program - Build an Inventory Management Program

Tell us what’s happening:

Trying to figure it out: Heeellllppp!

addProduct({name: “FLOUR”, quantity: 5}) should push {name: “flour”, quantity: 5} to the inventory array when no object having name equal to “flour” is found in the inventory.
16. removeProduct(“FLOUR”, 10) should log Not enough flour available, remaining pieces: 5 when inventory is equal to [{name: “flour”, quantity: 5}, {name: “rice”, quantity: 5}].

Your code so far

let inventory = [];

function findProductIndex(productName){
 return inventory.findIndex(item =>item.name === productName.toLowerCase());
}

function addProduct(productObj){
 let productIndex = findProductIndex(productObj.name);
 let productName = productObj.name.toLowerCase();
 
 if(productIndex !== -1){
   inventory[productIndex].quantity += productObj.quantity;
   console.log(`${productName} quantity updated`);
    
    }else{
      inventory.push({name: productName, quanity: productObj.quantity});
      console.log(`${productName} added to inventory`);

    }
  }

function removeProduct(productName, productQuantity){
  let productIndex = findProductIndex(productName);
  let normalizedName = productName.toLowerCase();

  if (productIndex === -1) {
    console.log(`${normalizedName} not found`);
    return;

  }

  let item = inventory[productIndex];
  item.quantity -= productQuantity;

  if(item.quantity <= 0){
    console.log(`Not enough ${item.name} available, removing from inventory`);
    inventory.splice(productIndex, 1);
  }else{
    console.log(`Remaining ${item.name} pieces: ${item.quantity}`);
  }
} 
inventory.push({name: "Flour", quantity: 15});
inventory.push({name: "rice", quantity: 10});
inventory.push({name: "sugar", quantity: 5});

console.log




Your browser information:

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

Challenge Information:

Build an Inventory Management Program - Build an Inventory Management Program

Hi @BooBoo212

When I view the inventory object I can see why flour is not found.

Happy cdoing

The issue lies within the removeProduct function, specifically with the if (item.quantity <= 0) block.

The problem is that the console.log message inside the if (item.quantity <= 0) condition is currently:
console.log(Not enough ${item.name} available, removing from inventory);

However, the exercise states that for removeProduct("FLOUR", 10) when the inventory is [{name: "flour", quantity: 5}, {name: "rice", quantity: 5}], it should log:
Not enough flour available, remaining pieces: 5

The current code’s console.log for item.quantity <= 0 does not match the expected output. It should be logging the remaining quantity, not a message about removing from inventory, when the quantity is reduced but not yet zero. The logic for logging “Remaining…” is in the else block, but the test case expects it even when item.quantity <= 0 but not yet fully removed.

The discrepancy is that the console.log message for the if (item.quantity <= 0) condition is not what the test case expects for the specific scenario where the quantity becomes negative but the item is still in the array before splice is called.

1 Like

Can you tell me why I am stuck on step 14? Everything is coming out correct in my console:

 let inventory = [];

function findProductIndex(productName){
  if(!productName || typeof productName !=="string") return -1;
   return inventory.findIndex(product => product.name.toLowerCase() === productName.toLowerCase());
}

function addProduct(product){
  if(!product || typeof product.name !=="string" || typeof product.quantity !=="number") {
    console.log("Invalid product object");
    return;
  }
  const productName = product.name.toLowerCase();
  const index = findProductIndex(productName);

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

function removeProduct(productName, quantity){
  const index = findProductIndex(productName);
  if(index === -1){
    console.log(`${productName.toLowerCase()} not found`);
    return;

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

  }else{
    inventory[index].quantity -=quantity;

    if(inventory[index].quantity <= 0){
      inventory.splice(index, 1);

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

  }

}
console.log("Current Inventory:", inventory);

//test

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

removeProduct("FLOUR", 5);
inventory.push({name: "flour", quantity: 20}, {name: "rice", quantity: 5});
removeProduct("flour", 5);

inventory.push({name: "flour", quantity: 5}, {name: "rice", quantity: 5});
removeProduct("FLOUR", 10);

I’ve edited your post to improve the readability of the code. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

it says that in the console there should be Remaining flour pieces: 15, right?

If I create the condition of the test,

inventory.length = 0;
inventory.push({name: "flour", quantity: 20}, {name: "rice", quantity: 5});
removeProduct("FLOUR", 5);

what I see in the console is Remaining: flour pieces: 15, which is not the same

do you see the difference?

removeProduct("FLOUR", 5)

should log

Remaining flour pieces: 15

to the console, when

inventory

is equal to

[{name: "flour", quantity: 20}, {name: "rice", quantity: 5}]

. // tests completed // console output Current Inventory: [ { name: ‘flour’, quantity: 15 }, { name: ‘rice’, quantity: 5 } ] flour added to inventory flour added to inventory rice added to inventory Remaining: flour pieces: 15 Not enough flour available, remaining pieces: 15

Nope! I can’t see anything anymore; I’ve been working on it for so long… :rofl:

Compare the sentences right next to each other in fixed width like this:

Remaining flour pieces: 15
Remaining: flour pieces: 15

It can help to more easily see the difference.

1 Like

Thank you!!! Finally… I got it through!!

1 Like