Build an Inventory Management Program - Build an Inventory Management Program

Tell us what’s happening:

Hello,
tests 3,4 and 12 are failing and yet the expected results show in the console. I’m having trouble understanding if it’s the conditional statements that are incorrect or if it’s something else. I think I can’t compare name of something that doesn’t exist in the array as an element i.e. inventory[i][“name”]. Can I get some help with this?

Edit: Code updated still same tests not passing

Your code so far

let inventory = [];

inventory = []

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


console.log(inventory)
console.log(findProductIndex("flour"));




function addProduct(prodObj) {
  let count = 0;
  if (inventory.length > 0) {
  for(let i = 0;i < inventory.length; i++) {
    
    if(prodObj["name"].toLowerCase() === inventory[i]["name"] && inventory[i]["quantity"] === undefined) {
      inventory[i]["quantity"] = 0
      inventory[i]["quantity"] += prodObj["quantity"]
      return console.log(prodObj["name"].toLowerCase() + " " + "quantity updated")
    }
    
else if(prodObj["name"].toLowerCase() === inventory[i]["name"] && inventory[i]["quantity"] !== undefined) {
  inventory[i]["quantity"] += prodObj["quantity"];
  return console.log(prodObj["name"].toLowerCase() + " " + "quantity updated");
}

else if (prodObj["name"].toLowerCase() !== inventory[i]["name"] && count == inventory.length - 1) {
  inventory.push(prodObj)
  prodObj["name"] = prodObj["name"].toLowerCase()
  return console.log(prodObj["name"].toLowerCase() + " " + "added to inventory");
}
count++
  }
  }
  else if (inventory.length == 0) {
  inventory.push(prodObj)
  prodObj["name"] = prodObj["name"].toLowerCase()
  return console.log(prodObj["name"].toLowerCase() + " " + "added to inventory");
  }

}





function removeProduct(prodName, prodQuant) {
  let count = 0;
  if(inventory.length > 0) {
for(let i = 0;i < inventory.length; i++) {
    
    if(prodName.toLowerCase() === inventory[i]["name"] && inventory[i]["quantity"] === undefined) {
      return "quantity not declared";
    }

    if(prodName.toLowerCase() === inventory[i]["name"] && inventory[i]["quantity"] !== undefined) {
  inventory[i]["quantity"] -= prodQuant;

  if(inventory[i]["quantity"] === 0) {
     inventory.splice(i,1);
     return console.log(prodName.toLowerCase() + " " + "removed")

  }
  else if (inventory[i]["quantity"] < 0) {
    inventory[i]["quantity"] += prodQuant;
  return console.log("Not enough" + " " + inventory[i]["name"] + " " + "available, remaining pieces:" + " " + inventory[i]["quantity"]); 
  }

  else if (inventory[i]["quantity"] > 0) {
    return console.log("Remaining" + " " + inventory[i]["name"] + " " + "pieces: " + inventory[i]["quantity"]);
  }

}

else if (prodName.toLowerCase() !== inventory[i]["name"] && count == inventory.length) {
  return console.log(prodName.toLowerCase() + " " + "not found");
}
count++
}
  }
  else if(inventory.length == 0) {
    return prodName.toLowerCase() + " " + "not found";
  }
}
console.log(removeProduct("FLOUR", 5))
console.log(inventory)

addProduct({name: "FLOUR", quantity: 5})
addProduct({name: "FLOUR", 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/146.0.0.0 Safari/537.36

Challenge Information:

Build an Inventory Management Program - Build an Inventory Management Program

it is good to tackle these one by one.

Let’s look at this function for eg:

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

To test it, you can change your inventory to a hardcoded array like:
let quantity = [ { name: 'flour', quantity: 5 } ]; // change it on line number 1

Then try to call findProductIndex(“flour”);
And see what it gives. Try and read through to see what happened.

Edit: if you can get it working, you can try another test. Use an inventory with 2 values for eg and try to retrieve the 2nd value

1 Like

Hi @MantasCodes ,

If inventory is [{name: "flour", quantity: 5}]

What do you see in the console when you run this code?

let index = findProductIndex("FloUr");
console.log("index:", index);

Happy coding!

1 Like

Thank you both very much for the help :slight_smile: added in a counter and removed the console.log() code from the return statement. Step 3 and 4 are now passing.

function findProductIndex(prodName) {
  let count = 0;
  if(inventory.length > 0){
for(let i = 0;i< inventory.length; i++) {
  if(prodName.toLowerCase() === inventory[i]["name"]) {
    return i;
  }
  else if(prodName.toLowerCase() !== inventory[i]["name"] && count == inventory.length - 1) {
    return -1;
  }
  count++
}
  }
  else if (inventory.length == 0) {
    return -1;
  }
}