Build an Inventory Management Program - Build an Inventory Management Program

Tell us what’s happening:

Hello,

I am looking at the logs of my code and seems that it is working as expected from the instructions, but it still does not pass the tests.

I am specially surprised that the findProduct does not pass, when it finds the item and is case insensitive as expected.

Your code so far

let inventory = [];

function findProductIndex(name){
  name = name.toLowerCase();
  for(let it in inventory){
    if(inventory[it]["name"] == name){return it;}
  }
  return -1;
}

function addProduct(prod){
  let ix = findProductIndex(prod["name"]);
  if(ix == -1){
    prod["name"] = prod["name"].toLowerCase();
    inventory.push(prod);
    console.log(`${prod["name"]} added to inventory`) 
  }
  else{ 
    inventory[ix]["quantity"] += prod["quantity"];
    console.log(`${inventory[ix]["name"]} quantity updated`)  
  }
  
  return;
}

function removeProduct(prod){
  let ix = findProductIndex(prod["name"])
  if(ix == -1){
    console.log(`${prod["name"]} not found`) 
  }
  else{ 
    let remainder = inventory[ix]["quantity"] - prod["quantity"];
    if(remainder<0){
      console.log(`Not enough ${inventory[ix]["name"]} available, remaining pieces: ${inventory[ix]["quantity"]}`);
    }
    else if(remainder ==0){
      inventory.pop(ix);
    }
    else{
      inventory[ix]["quantity"] = remainder;
      console.log(`${inventory[ix]["name"]} quantity updated`);
    }
     
  }
  
  return;
}

addProduct({"name": "sugar", "quantity":4});
addProduct({"name": "FLour", "quantity":5});

//console.log(inventory);
addProduct({"name": "flOur", "quantity":5});
//console.log(inventory);
removeProduct({"name": "flOur", "quantity":11});
let ix = findProductIndex("FLOUR")
//console.log(inventory[ix]["name"])
addProduct({"name": "sugar", "quantity":4});
removeProduct({"name": "sugar", "quantity":9});
removeProduct({"name": "sugar", "quantity":8});
removeProduct({"name": "fruit", "quantity":8});
console.log(inventory);
console.log(findProductIndex("fruit"))

Your browser information:

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

Challenge Information:

Build an Inventory Management Program - Build an Inventory Management Program

hello!

the index returned by findProductIndex should be of type "number”. Your’s currently returns index as a string.

kindly recheck User Story#6 about what removeProduct takes as arguments.