Tell us what’s happening:
guys i need help from 14-16 test, i have no idea what’s wrong with my code
Your code so far
let inventory = [];
inventory[0] = { name: 'flour', quantity: 20 };
inventory[1] = { name: 'rice', quantity: 2 };
let empty = [];
function findProductIndex(product) {
let lowerCase = product.toLowerCase();
for (let i = 0; i < inventory.length; i++) {
if (inventory[i].name === lowerCase) {
return inventory.indexOf(inventory[i]);
}
}
for (let i = 0; i < inventory.length; i++) {
if (inventory[i].name !== lowerCase) {
return -1;
}
}
}
console.log(findProductIndex('flou'));
function addProduct(product) {
let lowerCase = product.name.toLowerCase();
for (let i = 0; i < inventory.length; i++) {
if (inventory[i].name === lowerCase) {
inventory[i].quantity = inventory[i].quantity + product.quantity;
console.log(`${inventory[i].name} quantity updated`);
}
}
for (let i = 0; i < inventory.length; i++) {
for (let x of inventory[i].name.split(' ')) {
empty.push(x);
}
}
if (!empty.includes(lowerCase)) {
product.name = lowerCase;
inventory.push(product);
console.log(`${lowerCase} added to inventory`)
}
}
console.log(addProduct({ name: "FLour", quantity: 0 }))
function removeProduct(product, quantity) {
let lowerCase = product.toLowerCase();
for (let i = 0; i < inventory.length; i++) {
if (lowerCase === inventory[i].name) {
inventory[i].quantity = inventory[i].quantity - quantity;
console.log(`Remaining ${inventory[i].name} pieces: ${inventory[i].quantity}`);
if (inventory[i].quantity < quantity) {
console.log(`Not enough ${inventory[i].name} available, remaining pieces: ${inventory[i].quantity}`)
}
}
if (inventory[i].quantity === 0) {
delete inventory[i];
}
}
if (!empty.includes(lowerCase)) {
console.log(`${lowerCase} not found`)
}
}
console.log(removeProduct("FLoUR", 20))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 OPR/124.0.0.0
Challenge Information:
Build an Inventory Management Program - Build an Inventory Management Program