Build an Inventory Management Program - Build an Inventory Management Program

Tell us what’s happening:

I can not go through tests 3,4,7,8,13,14,16 . Someone assist . What could be wrongth with my code.

Your code so far

let inventory=[];
function findProductIndex(productName){
  for(let i=0;i<inventory.length;i++){
    if (inventory[i].name===productName.toLowerCase())
    {
      return i
    }else{
      return -1
    }
  }
}
function addProduct(product) {
    product.name = product.name.toLowerCase();
    
    const index = findProductIndex(product.name);
    
    if (index !== -1) {
        inventory[index].quantity += product.quantity;
        console.log(`${product.name} quantity updated`);
    } else {
        inventory.push(product);
        console.log(`${product.name} added to inventory`);
    }
}

function removeProduct(productName, quantity) {
    const lowercaseName = productName.toLowerCase();
    const index = findProductIndex(lowercaseName);
    
    if (index === -1) {
        console.log(`${lowercaseName} not found`);
        return;
    }
    
    const product = inventory[index];
    
    if (product.quantity < quantity) {
        console.log(`Not enough ${product.name} available, remaining pieces: ${product.quantity}`);
        return;
    }
    
    product.quantity -= quantity;
    
    if (product.quantity === 0) {
  
        inventory.splice(index, 1);
        console.log(`Remaining ${product.name} pieces: 0 (removed from inventory)`);
    } else {
        console.log(`Remaining ${product.name} pieces: ${product.quantity}`);
    }
}

Your browser information:

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

Challenge Information:

Build an Inventory Management Program - Build an Inventory Management Program

You don’t need an else statement in your findProductIndex function. Put the return statement for -1 outside the for loop and your code should pass.

what have you tried for debugging? what do you think is the issue?