Build an Inventory Management Program - Build an Inventory Management Program

Tell us what’s happening:

I think I’m missing some minor thing that is driving me nuts. Test 8, 10, 12, 14 and 16 keep failing even though the output is exactly what is expected. I have spent hours over the last few days trying different things but haven’t narrowed the failures down further.

Your code so far

const inventory = [];

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

const addProduct = (prodObj) => {
  const index = findProductIndex(prodObj.name.toLowerCase());
  //confirm exists
  if(index !== -1){
    inventory[index].quantity += prodObj.quantity;
    return `${prodObj.name.toLowerCase()} quantity updated`;
  }
  else {
    inventory.push({name: prodObj.name.toLowerCase(), quantity: prodObj.quantity});
    return `${prodObj.name.toLowerCase()} added to inventory`;
  }
}

const removeProduct = (name, quantity) => {
  const index = findProductIndex(name.toLowerCase());
  //confirm exists
  if(index !== -1){
    //check removed is not greater than quantity
    if(inventory[index].quantity < quantity){
      return `Not enough ${name.toLowerCase()} available, remaining pieces: ${inventory[index].quantity}`;
    }
    else{
      //check amount left before updating inventory
      const left = inventory[index].quantity - quantity;
      if(left === 0){
        inventory.splice(index, 1);
      }
      if(left > 0){
        inventory[index].quantity -= quantity;
        return `Remaining ${name.toLowerCase()} pieces: ${inventory[index].quantity}`
      }
  }
  }
  else {
    return `${name.toLowerCase()} not found`
  }
}

console.log(removeProduct("FLOUR", 5)) //testing #12
console.log(addProduct({name: "FLOUR", quantity: 5})) //testing #10
addProduct({name: "Rice", quantity: 5}) //added for testing
console.log(inventory) //show inventory prior to test #16
console.log(removeProduct("FLOUR", 10)) //testing #16
console.log(addProduct({name: "FLOUR", quantity: 5})) //testing #8
addProduct({name: "FLOUR", quantity: 10}) //added for testing
console.log(inventory)//show inventory prior to test #14
console.log(removeProduct("FLOUR", 5)) //testing #14

Your browser information:

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

Challenge Information:

Build an Inventory Management Program - Build an Inventory Management Program

Here are some debugging steps you can follow. Focus on one test at a time:

  1. Are there any errors or messages in the console?
  2. What is the requirement of the failing test?
  3. Check the related User Story and ensure it’s followed precisely.
  4. What line of code implements this?
  5. What is the result of the code and does it match the requirement? (Write the value of a variable to the console at that point in the code if needed.)

If this does not help you solve the problem, please reply with answers to these questions.

Check the user stories carefully. Are you supposed to be returning a message or logging a message?

Thanks for the tip. I realized the stories said to log (console.log) the results of the functions, not return a string to be logged in an outside console log. Everything passed after making the change from return to console.log instead.