Build an Inventory Management Program - Build an Inventory Management Program

Tell us what’s happening:

I’ve tried everything but it fails on tests 12-16. When I check it in online compiler it renders same results needed

Your code so far

let inventory = [];

function findProductIndex(prodName) {
  let chosenProd;
  for (const prod of inventory) {
    if (prod.name == prodName.toLowerCase()) {
      chosenProd = prod;
    };
  };
  return inventory.indexOf(chosenProd)
};

function addProduct(prodObj) {
  let notFound = true;
  for (let prod of inventory) {
    if (prod.name.toLowerCase() == prodObj.name.toLowerCase()) {
      notFound = false;
      prod.quantity += prodObj.quantity
      console.log(`${prod.name.toLowerCase()} quantity updated`)
      break
    };
  };
  if (notFound) {
    let lowName = prodObj.name.toLowerCase()
    let newObj = {
      name: lowName,
      quantity: prodObj.quantity
    }
    inventory.push(newObj);
    console.log(`${newObj.name} added to inventory`)
  }
};

//6
function removeProduct(prodName, quantity) {
  let notFound = true;
  let chosenProd;
  for (prod of inventory) {
    if (prod.name.toLowerCase() == prodName.toLowerCase()) {
      notFound = false;
      chosenProd = prod;
      let result = chosenProd.quantity - quantity;
      if (result == 0) {
        let prodIndex = inventory.indexOf(chosenProd);
        inventory.splice(prodIndex, 1)
      } else if (result < 0) {
        console.log(`Not enough ${chosenProd.name} available, remaining pieces: ${chosenProd.quantity}`)
      } else if (result > 0) {
        chosenProd.quantity = result;
        console.log(`Remaining ${chosenProd.name} pieces: ${chosenProd.quantity}`)
      }
    };
  };
  if (notFound) {
    console.log(`${prodName.toLowerCase()} not found`)
  };
};

Your browser information:

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

Challenge Information:

Build an Inventory Management Program - Build an Inventory Management Program

if you add

inventory.length = 0;
inventory.push({name: 'flour', quantity: 15});
inventory.push({name: 'rice', quantity: 10});
inventory.push({name: 'sugar', quantity: 5});
removeProduct("SALT", 2);
removeProduct("Spam", 1);

below your code, you should see what’s the issue

Thank you, the output in this case was truly wrong. And the reason for it was as ever in one little flaw: I haven’t used LET or CONST in for (prod of inventory) (thank to AI I found it). Thanks again and btw, how do I add formatted code in my answer like you did?

When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like