Build an Inventory Management Program - Build an Inventory Management Program

Tell us what’s happening:

Hey,there. Here is my code, which works in console, but I can’t pass tests 8, 9, 12, 13, 14, 16
I can’t understand why everyting works.For example, when I add values to the inventory, console shows the correct answers

Your code so far

let inventory = []

function findProductIndex(productname){
  productname = productname.toLowerCase()
  for(let object of inventory){
    if(object.name == productname){
   return inventory.indexOf(object)
  } 
}return -1
}

function addProduct(productObject){
 
  for(let object of inventory){
    if(object.name == productObject.name.toLowerCase()){
object.quantity = object.quantity + productObject.quantity
return `${object.name} quantity updated`
    }
  }if (inventory.includes(productObject)== false){
  productObject.name = productObject.name.toLowerCase()
  inventory.push(productObject)
  return `${productObject.name} added to inventory`
  }
}

function removeProduct(productName, quantity){
  productName = productName.toLowerCase()
   
  for(let object of inventory){
    
    if(object.name == productName){
      let newQuantity = object.quantity - quantity
      if(newQuantity > 0){
        return `Remaining ${object.name} pieces: ${newQuantity}`
      }else if( newQuantity === 0){
        let index=findProductIndex(productName)
        inventory.splice(index,1)
        console.log(inventory)
      }else if(newQuantity < 0){
        return `Not enough ${object.name} available, remaining pieces: ${object.quantity}`
      }
    }
  }
  return`${productName} not found`
  }

console.log(inventory)
console.log(removeProduct("FLOUR", 5))







Your browser information:

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

Challenge Information:

Build an Inventory Management Program - Build an Inventory Management Program

if I add addProduct({name: "FLOUR", quantity: 5}) at the end of your code I do not see “flour quantity updated” at the end of the code, do you see that?

Yes, it shows “flour added to inventory”, cause array is empty.But if I add object {name: “flour”} to the inventory array, it will be updated. Shouldn’t i add anything to the inventory array?

Review User Story 4.

addProduct({name: “FLOUR”, quantity: 5}) is not the same as console.log(addProduct({name: “FLOUR”, quantity: 5}))

Sorry,I can’t understand what you mean

…and log to the console the product name followed by a space and quantity updated .

Are you doing this?

1 Like

now I got it) thanks a lot)