Tell us what’s happening:
here is my problem
8. addProduct({name: “FLOUR”, quantity: 5}) should log flour quantity updated when an object having name equal to “flour” is found in the inventory array.
how to solve this?
Your code so far
let inventory = [];
function findProductIndex(productName) {
const lowerName = productName.toLowerCase();
return inventory.findIndex(product => product.name === lowerName);
}
function addProduct(product) {
const index = findProductIndex(product.name);
if(index !== -1) {
inventory[index].quantity += product.quantity;
console.log(inventory[index].name + " " + inventory[index].quantity);
console.log(inventory[index].name) + " quantity updated";
} else {
product.name = product.name.toLowerCase();
inventory.push(product);
console.log(`${product.name} added to inventory`);
}
}
function removeProduct(productName, quantity) {
const index = findProductIndex(productName);
const lowerName = productName.toLowerCase();
if(index === -1) {
console.log(`${lowerName} not found`);
return;
}
const currentProduct = inventory[index];
if(currentProduct.quantity < quantity) {
console.log(`Not enough ${currentProduct.name} available, remaining pieces: ${currentProduct.quantity}`)
} else {
currentProduct.quantity -= quantity;
console.log(`Remaining ${currentProduct.name} pieces: ${currentProduct.quantity}`)
if(currentProduct.quantity === 0) {
inventory.splice(index, 1);
}
}
}
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