Tell us what’s happening:
I am having issues with the findProductIndex() function working properly. It works just fine when you call it and the array is empty. However, when it includes an element it still returns -1. For instance, I could use the following:
console.log(addProduct({name: "FLOUR", quantity: 5}));
I ensure that the empty array contains an element before calling findProductIndex(), but it still returns -1 for some reason.
INITIAL PROBLEM | SOLVED
I am having issues parsing through an array of objects to determine if a property of an object such as name exists within the array. The issue I’m referring to can be found in the removeProduct() function towards the bottom where I added some debugging statements. I made an attempt with converting the array to a JSON string in order to make parsing more easier since its a string. However, when I use the .includes() method it doesn’t work as intended. I am unsure as to where to go from where since this is the only issue I’m having.
Your code so far
let inventory = [];
function findProductIndex(productName) {
productName = productName.toLowerCase();
for (const element of inventory) {
if (element.name === productName) {
return inventory(element);
}
}
return -1;
}
function addProduct(productObject) {
productObject.name = productObject.name.toLowerCase();
for (const element of inventory) {
if (productObject.name === element.name) {
element.quantity += productObject.quantity;
console.log(`${productObject.name} quantity updated`);
break;
} else {
inventory.push(productObject);
console.log(`${productObject.name} added to inventory`);
break;
}
}
}
function removeProduct(productName, productQuantity) {
productName = productName.toLowerCase();
for (const element of inventory) {
// REMOVE THIS WHEN DONE || DEBUGGING STATEMENT
console.log("Product Name: " + element.name);
if (element.name === productName) {
element.quantity -= productQuantity;
if (element.quantity === 0) {
inventory.pop(element);
} else if (element.quantity < 0) {
console.log(`Not enough ${element.name} available, remaining pieces: ${element.quantity}`)
}
console.log(`Remaining ${element.name} pieces: ${element.quantity}`);
}
}
console.log("\n#### DEBUGGING ####");
const masterString = JSON.stringify(inventory);
console.log("Inventory String: " + masterString);
console.log("Product Name: " + productName);
console.log("Does it exist in inventory? " + masterString.includes(productName));
// console.log(`${productName} not found`); || MAIN ISSUE WITH FINDING THIS NAME IN AN ARRAY OF OBJECTS
}
// DEBUGGING
console.log("\n" + removeProduct("floUr", 1));
console.log("Current Inventory: " + inventory);
Console Output:
Product Name: flour
Remaining flour pieces: 4
#### DEBUGGING ####
Inventory String: [{"name":"flour","quantity":4}]
Product Name: flour
Does it exist in inventory? true
undefined
Current Inventory: [object Object]
The test cases don’t ask for any of the functions to have a return statement except findProductIndex() which is why undefined is seen in the console. Also, I just now realized that the console output for “Current Inventory: “ has changed. Initially, it printed out the array of elements, but now it says "[object Object]”. I don’t know why this had changed. I thought this may have been to due shallow copy by reference due to using JSON.stringify(), however I commented those debugging statements out and it still printed out as "[object Object]”.
EDIT:
I don’t know what happened, but I made a minor change and it works now. However, I still do not know why inventory is printing out as "[object Object]”.
Challenge Information:
Build an Inventory Management Program - Build an Inventory Management Program