Tell us what’s happening:
My function is very much getting the correct things logged and doing the correct maths, why is test 13, 14, and 16 failing?
Your code so far
let inventory = [{name: "flour", quantity: 20}, {name: "rice", quantity: 5}];
function findProductIndex(proName){
for (let i = 0; i < inventory.length; i++){
if (proName.toLowerCase() == inventory[i].name){
return i
}
}
return -1
}
//Good 2 here
const addProduct = (object) => {
let productName = object.name.toLowerCase();
object.name = object.name.toLowerCase();
if (findProductIndex(object.name) == -1) {
inventory.push(object);
console.log(`${productName} added to inventory`)
}
else {
let added = object.quantity
for (let i = 0; i < inventory.length; i++){
if (productName == inventory[i].name){
inventory[i].quantity += added;
console.log(`${inventory[i].name} quantity updated`)
}
}
}
};
//Good to Here
const removeProduct = (productName, productQuantity) => {
let index = findProductIndex(productName);
productName = productName.toLowerCase();
if (index == -1){
console.log(`${productName} not found`)
}
else {
for (let i = 0; i < inventory.length; i++) {
if ((inventory[i].quantity - productQuantity) < 0){
console.log(`Not enough ${productName} available, remaining pieces: ${inventory[i].quantity}`)
}
else if ((inventory[i].quantity - productQuantity) == 0){
inventory.splice(index, 1);
}
else if ((inventory[i].quantity - productQuantity) > 0){
inventory[i].quantity -= productQuantity;
console.log(`Remaining ${productName} pieces: ${inventory[i].quantity}`)
}
}
}
}
console.log(removeProduct("FLOUR", 5))
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36
Challenge Information:
Build an Inventory Management Program - Build an Inventory Management Program
