Build an Inventory Management Program - Build an Inventory Management Program

Tell us what’s happening:

Very strange, I can’t pass test points 3 and 4, and everything else is fine. I also compared other passed codes, among which findProductIndex code has the same function.

Your code so far

let inventory=[]

function findProductIndex(name)
{
  name=name.toLowerCase();
  for(let i in inventory)
  {
    let curName=inventory[i].name.toLowerCase();
    if(curName===name)return i;
  }
  return -1;
}



function addProduct(produce)
{
  produce.name=produce.name.toLowerCase();
  let index=findProductIndex(produce.name);
  if(index===-1)
  {
    inventory.push(produce);
    console.log(produce.name+" added to inventory");
  }else
  {
    inventory[index].quantity+=produce.quantity;
    console.log(produce.name+" quantity updated");
  }
}


function removeProduct(name,quantity)
{
  name=name.toLowerCase();
  let index=findProductIndex(name);
  if(index===-1)
  {
    console.log(name+" not found");
  }else 
  {
    if(inventory[index].quantity===quantity)
    {
      inventory.splice(index,1);
    }else if(inventory[index].quantity<quantity)
    {
        console.log(`Not enough ${name} available, remaining pieces: ${inventory[index].quantity}`)
    }else
    {
      inventory[index].quantity-=quantity;
      console.log(`Remaining ${name} pieces: ${inventory[index].quantity}`)
    }
  }
}



Your browser information:

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

Challenge Information:

Build an Inventory Management Program - Build an Inventory Management Program
https://www.freecodecamp.org/learn/full-stack-developer/lab-inventory-management-program/build-an-inventory-management-program

  • try debug what happens to those given use cases? for example `console.log(findProductIndex(“flour”))` try that at end of your codebase and troubleshoot that method and match your expected output

happy coding :slight_smile:

When I add to the inventory array
"

{
name:“FLOUR”,
quantity:10
},
{
name:“flour”,
quantity:10
},
{
name:“no”,
quantity:10
}

"After that, I executed at the end of the code in turn.


console.log(findProductIndex(“flour”))
console.log(findProductIndex(“no”))
console.log(findProductIndex(“yes”))

They output separately.

0
2
-1

I think this output is correct.

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 three backticks to make it easier to read.

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

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

Are you using addProduct() to add those to the inventory?

Shouldn’t “no” index be 1?

Use f12 to open the devtools console and see a better error message.

 actual: "0", expected: 0,

You are right. I used the addProduct function this time.
I added these statements separately at the end,

addProduct(
  {
    name:"FLOUR",
    quantity:10
  }
)
addProduct(
  {
    name:"flour",
    quantity:15
  }
)
addProduct(
  {
    name:"no",
    quantity:11
  }
)

console.log(findProductIndex("flour"))
console.log(findProductIndex("no"))
console.log(findProductIndex("yes"))

console.log(inventory)

and this is the output result.

flour added to inventory
flour quantity updated
no added to inventory
0
1
-1
[ { name: 'flour', quantity: 25 },
  { name: 'no', quantity: 11 } ]

I think this should be in line with expectations.

if this is not resolved yet, share your updated code, thanks and happy coding :slight_smile:

let inventory=[]

function findProductIndex(name)
{
  name=name.toLowerCase();
  for(let i in inventory)
  {
    let curName=inventory[i].name.toLowerCase();
    if(curName===name)return i;
  }
  return -1;
}



function addProduct(produce)
{
  produce.name=produce.name.toLowerCase();
  let index=findProductIndex(produce.name);
  if(index===-1)
  {
    inventory.push(produce);
    console.log(produce.name+" added to inventory");
  }else
  {
    inventory[index].quantity+=produce.quantity;
    console.log(produce.name+" quantity updated");
  }
}


function removeProduct(name,quantity)
{
  name=name.toLowerCase();
  let index=findProductIndex(name);
  if(index===-1)
  {
    console.log(name+" not found");
  }else 
  {
    if(inventory[index].quantity===quantity)
    {
      inventory.splice(index,1);
    }else if(inventory[index].quantity<quantity)
    {
        console.log(`Not enough ${name} available, remaining pieces: ${inventory[index].quantity}`)
    }else
    {
      inventory[index].quantity-=quantity;
      console.log(`Remaining ${name} pieces: ${inventory[index].quantity}`)
    }
  }
}

addProduct(
  {
    name:"FLOUR",
    quantity:10
  }
)
addProduct(
  {
    name:"flour",
    quantity:15
  }
)
addProduct(
  {
    name:"no",
    quantity:11
  }
)

console.log(findProductIndex("flour"))
console.log(findProductIndex("no"))
console.log(findProductIndex("yes"))
  • do you need to make “curName” lowercased?
  • look into other array method (i.e. js array methods) that returns an “index” when found and try to incorporate that instead of “for..in”

happy coding :slight_smile: