Tell us what’s happening:
- addProduct({name: “FLOUR”, quantity: 5}) should add 5 to quantity value of the object having name equal to “flour” in the inventory array.
- addProduct({name: “FLOUR”, quantity: 5}) should push {name: “flour”, quantity: 5} to the inventory array when no object having name equal to “flour” is found in the inventory.
I have problems with this two test, but it checking alll right< thanks for asttantion
Your code so far
let inventory = [];
function findProductIndex(name) {
for (let i = 0; i < inventory.length; i++) {
if(inventory[i].name.toLowerCase() === name.toLowerCase()) {
return i;
}
}
return -1;
}
function addProduct(product) {
let index = findProductIndex(product.name);
if (index > -1) {
inventory[index].quantity = product.quantity;
console.log(product.name.toLowerCase() + " " + "quantity updated");
}else{
inventory.push(product);
console.log(product.name.toLowerCase() + " " + "added to inventory")
}
}
let product1 = {name: "Amur", quantity: 5};
let product2 = {name: "flour", quantity: 5};
inventory.push(product1, product2)
console.log(findProductIndex("FLOUR"))
addProduct({name: "Kaka", quantity: 6});
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/135.0.0.0 Safari/537.36
Challenge Information:
Build an Inventory Management Program - Build an Inventory Management Program