Build an Inventory Management Program - Build an Inventory Management Program

Tell us what’s happening:

iam lost i donno how to fulfill the condition to push the obj that is not exist on the array on addProduct function

Your code so far

let inventory = [
    {name: 'pasta', quantity: 15},
    {name: 'flour', quantity: 15},
  ]

function findProductIndex(productName) {
  for(let i = 0; i < inventory.length; i++) {
    if(inventory[i].name == productName.toLowerCase()) {
      return i
    }
  }
      return -1
}

function addProduct(obj) {
  let bool = false
  for(let i = 0; i < inventory.length; i++) {

  
    if(inventory[i].name == obj.name.toLowerCase()) {
      inventory[i].quantity += obj.quantity
      console.log(`${inventory[i].name} quantity updated`)
    } 

  if(!inventory[i].name == obj.name && !bool) {

      inventory.push(obj)
      bool = true

    }

  
  }  
  return inventory
}


console.log(addProduct({name: "ChocolaTe", 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/139.0.0.0 Safari/537.36

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

Here are some troubleshooting steps you can follow. Focus on one test at a time:

  • What is the requirement of the first failing test?
  • Check the related User Story and ensure it’s followed precisely.
  • What line of code implements this?
  • What is the result of the code and does it match the requirement? (Write the value of a variable to the console at that point in the code if needed.)
  • Are there any errors or messages in the console?

If this does not help you solve the problem, please reply with answers to these questions.

yea , iam trying but i can’t reach the result that i want . look i am checking for the property name while looping on the obj that i add ,
the first condition if it’s exist i add the quantity to the quantity that in the obj , the second conition if not exist i should push the obj in the array
and that’s what i fail in

what method are you using to find out if the item is already present in the inventory or not?

I have issues following your logic, can you explain to me how you are checking that?

wait i send you the piece of code

let bool = false
 if(!inventory[i].name == obj.name.toLowerCase() && !bool) {

      inventory.push(obj)
      bool = true 

    }

are you able to explain the logic in your own words?

i guess you can read it but you want to let me see something that i don’t see !
ok i will explain
i loop for all inventory objects.name if the obj that i try to add is not present i push all the obj on the array then i change the bool variable to true to prevent infinite happens , what you see ?

are you pushing during or after you loop all inventory object.name?

now i see something wrong that i did . i should put ! then () to negate all the condition to check on it now i did but i fall in the infinite although i change the bool to true on the statement

yes man now i did it i did it loooooook look i will share you my modified condition and how it success pushed the obj finaaaaaaaalyy

I do not understand what you did but if it works that’s great

but i need to explain it to you , sir limme do it pls

let bool = false;
if(!(inventory[i].name == obj.name.toLowerCase()) && !bool) {

      inventory.push(obj)

      bool = true
    }

now i check for if not the inventory objects have the object.name that i try to add and i will tell you why i use boolean variable

and that is inside your loop? how many items in the inventory are you checking before pushing?

if it’s inside your loop, it looks like it’s pushing when the first item does not match

all inventory indexes

you said it’s inside your loop, so that means that it checks the first one, inventory[i].name == obj.name.toLowerCase() is false, and the new element is pushed

what if the matching element is the last one in the inventory?

yes but there is a second statement i use to prevent the infinite that’s the point of using boolean variable that i created ? you got the point ??

cuz when i succeed push the obj i change the variable to true immediately

but i still feel there is something wrong because i do not pass the tests that i want