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