Tell us what’s happening:
“You should declare an empty array named inventory”
This is the first test and only text I didn’t pass. But everything seems fine to me.
Your code so far
const inventory = [];
function findProductIndex(name) {
let lowerName = name.toLowerCase();
for (let i = 0; i < inventory.length; i++) {
if (inventory[i].name.toLowerCase() === lowerName) {
return i;
}
}
return -1;
}
function addProduct(product) {
let finalPro = {
name: product.name.toLowerCase(),
quantity: product.quantity
}
for (let i = 0; i < inventory.length; i++) {
if (inventory[i].name === finalPro.name) {
console.log(inventory[i].name + " quantity updated");
inventory[i].quantity = inventory[i].quantity + product.quantity;
return
}
}
console.log(finalPro.name + " added to inventory")
inventory.push(finalPro);
return
}
function removeProduct(bigName, quantity) {
let name = bigName.toLowerCase();
for (let i = 0; i < inventory.length; i++) {
if (inventory[i].name === name) {
let newQuantity = inventory[i].quantity - quantity;
if (newQuantity === 0) {
inventory.splice(i, 1);
return
} else if (newQuantity < 0) {
console.log(`Not enough ${name} available, remaining pieces: ${inventory[i].quantity}`);
return
} else {
console.log("Remaining " + name + " pieces: " + newQuantity);
inventory[i].quantity = newQuantity;
return
}
}
}
console.log(name + " not found");
return
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/150.0.0.0 Safari/537.36 Edg/150.0.0.0
Challenge Information:
Build an Inventory Management Program - Build an Inventory Management Program