Build an Inventory Management Program - Build an Inventory Management Program

Tell us what’s happening:

Please I’m stuck and I don’t know how to continue, every test pass successfully except for 15 and I don’t know how to remove from the original inventory… Splice doesn’t modify the original inventory aswell please how do I complete this challenge??

Your code so far

const inventory = []

function findProductIndex(pName){
  let lowCase = pName.toLowerCase()
  let index = inventory.findIndex((name) => name.name === lowCase)
    return index
}

function addProduct(pName){
  let lowCase = pName.name.toLowerCase()
  let filter = inventory.filter((obj) => obj.name === lowCase)
  let ven = findProductIndex(lowCase)
  if(inventory[ven]){
    inventory[ven].quantity += pName.quantity
    console.log(`${lowCase} quantity updated`)
  }else if(!inventory[ven]){
    inventory.push({name : lowCase, quantity: pName.quantity})
  console.log(`${lowCase} added to inventory`)
  }
  return inventory
}

function removeProduct(pName,quantity){
  let lowCase = pName.toLowerCase()
  let remove = findProductIndex(lowCase)
  if(!inventory[remove]){
    console.log(`${lowCase} not found`)
  }
  if(inventory[remove]){
    if(inventory[remove].quantity === 0){
      inventory.splice(remove, 1)
    }if(inventory[remove].quantity !== 0 && inventory[remove].quantity < quantity){
      console.log(`Not enough ${lowCase} available, remaining pieces: ${inventory[remove].quantity}`)
    }else{
    inventory[remove]. quantity -= quantity
    console.log(`Remaining ${lowCase} pieces: ${inventory[remove].quantity}`)}
  }
  return inventory
}
console.log(removeProduct("FLOUR", 20))

Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Mobile Safari/537.36

Challenge Information:

Build an Inventory Management Program - Build an Inventory Management Program

Splice() does modify the original inventory in place. The issue is your logic in that function. Think about when you remove an inventory item.

[The splice() method of Array instances changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.](Array.prototype.splice() - JavaScript | MDN)

1 Like

expected logic:

  • subtract from quantity
  • 0? remove from inventory

your code:

  • 0? remove from inventory
  • subtract from quantity

it’s failed cuz the removal check is pre-mature. happy coding.

tip: the code is very painful to read due to 2 main reasons

  • repetitive lengthy syntax or confusing variable names
  • redundant if statements
    if you can find a way to optimize, it’ll be a huge improvement for readability.