Build an Inventory Management Program Tests: 3, 4, 7, 8, 13, 14, 15, 16 not passing

Well, the title explains.

  • Failed:3. findProductIndex("flour") should return the index of the object having name equal to "flour" inside the inventory array.

  • Failed:4. findProductIndex("FloUr") should return the index of the object having name equal to "flour" inside the inventory array.

  • Failed:7. addProduct({name: "FLOUR", quantity: 5}) should add 5 to quantity value of the object having name equal to "flour" in the inventory array.

  • Failed:8. addProduct({name: "FLOUR", quantity: 5}) should log flour quantity updated when an object having name equal to "flour" is found in the inventory array.

  • 13. removeProduct("FLOUR", 5) should subtract 5 from the quantity value of the object having name equal to "flour" inside the inventory array.

  • Failed:14. 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}].

  • Failed:15. If the quantity after the subtraction is zero, removeProduct should remove the product object from the inventory.

  • Failed: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}].

It seems I can’t get these tests to pass.

Here’s the code:

let inventory = []

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

function addProduct(obj) {
  let productIndex = findProductIndex(obj.name)
  if (productIndex !== -1) {
    let product = inventory[productIndex]
    product.quantity += obj.quantity
    obj.name = obj.name.toLowerCase()
    console.log(`${obj.name} quantity updated`)
  } else {
    obj.name = obj.name.toLowerCase()
    inventory.push(obj)
    console.log(`${obj.name} added to inventory`)
  }
}

function removeProduct(str, int) {
  let productIndex = findProductIndex(str)
  str = str.toLowerCase()
  if (productIndex == -1) {
    console.log(`${str} not found`)
  } else {
      let product = inventory[productIndex]
    if (product.quantity >= int) {
      product.quantity -= int
      console.log(`Remaining ${str} pieces: ${product.quantity}`)
      if (product.quantity == 0) {
        inventory.splice(productIndex, 1)
      }
    } else {
      console.log(`Not enough ${str} available, remaining pieces: ${product.quantity}`)
    }
  }
}

hello!

are you sure i should reach the case when it is actually equal to inventory.length(it is always 1 greater than the last index)?

what does findProductIndex return when the item you are trying to find, is not the first item in your inventory array?

addProduct({name: "rice", quantity: 5})
addProduct({name: "flour", quantity: 5});
console.log(findProductIndex("flour")); //expected output: 1

Thanks! I’ve solved it!

1 Like

Can you also tell what’s wrong with my function:

function findProductIndex(name) {

  for (let idx in inventory) {

if (inventory[idx].name == name) return idx;

}

return -1;

}

hi @lakshyakeshwani3

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Help button located on the challenge.

The Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.