Build an Inventory Management Program - Build an Inventory Management Program

Tell us what’s happening:

  1. addProduct({name: “FLOUR”, quantity: 5}) should add 5 to quantity value of the object having name equal to “flour” in the inventory array.
  2. 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

add this below your code:

inventory.length = 0;
inventory.push({name: 'flour', quantity: 15});
inventory.push({name: 'rice', quantity: 10});
inventory.push({name: 'sugar', quantity: 5});
addProduct({name: 'Flour', quantity: 5});
addProduct({name: 'RICE', quantity: 5});
addProduct({name: 'SuGar', quantity: 5});

console.log(inventory)

the final quantities should be 20 for flour, 15 for rice and 10 for sugar. Check the inventory at the end, does it have those values?

Story 2 says the findProductIndex function should always use the lowercase form of the product to perform the search. Are you doing this?

What is this doing:

inventory[index].quantity = product.quantity;

Story 1 says the object name should always be lowercase. Where is this in your else statement?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.