Hello, I’m very new to programming/coding so I apologize for the messy code. When I test my addProduct function for steps 8 and 10, I think the console logs the correct outputs, but I fail the tests. Step 9 however passes. Can someone help point me in the correct direction?
Your code so far
let inventory = [];
let soda = {name: "soda", quantity: 5};
let cheese = {name: "cheese", quantity: 3};
let crackers = {name: "crackers", quantity: 10};
let flour = {name: "flour", quantity: 5};
inventory.push(soda, cheese, crackers, flour);
function findProductIndex (name) {
let exists = 0;
let productIndex;
for (let i = 0; i < inventory.length; i++) {
if (name.toLowerCase() === inventory[i].name) {
productIndex = i;
exists++;
}
}
if (exists > 0) {
return productIndex;
}
else {
return -1;
}
};
function addProduct (productObj) {
let productName = productObj.name.toLowerCase();
let productIndex = findProductIndex(productName);
if (productIndex !== -1) {
inventory[productIndex].quantity += productObj.quantity;
console.log(`${productName} quantity updated`);
}
else {
inventory.push({name: productName, quantity: productObj.quantity});
console.log(`${productName} added to inventory`);
}
};
console.log(inventory);
addProduct({name: "FLOUR", quantity: 5});
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Challenge Information:
Build an Inventory Management Program - Build an Inventory Management Program
Hello again. I did as you suggested and I created the removeProduct function. Afterwards manually performing each test I get the correct output, but most of the tests fail when I run them. I included the outputs as comments at the end of the code.
let inventory = [];
function findProductIndex(productName) {
let exists = 0;
let productIndex;
for (let i = 0; i < inventory.length; i++) {
if (productName.toLowerCase() === inventory[i].name) {
productIndex = i;
exists++;
}
}
if (exists > 0) {
console.log(productIndex);
return productIndex;
}
else {
console.log(-1);
return -1;
}
}
function addProduct (productObj) {
let productName = productObj.name.toLowerCase();
let productIndex = findProductIndex(productName);
if (productIndex !== -1) {
inventory[productIndex].quantity += productObj.quantity;
console.log(`${productName} quantity updated`);
}
else {
inventory.push({name: productName, quantity: productObj.quantity});
console.log(`${productName} added to inventory`);
}
};
function removeProduct (productObj) {
let productName = productObj.name.toLowerCase();
let productIndex = findProductIndex(productName);
if (productIndex !== -1) {
if (inventory[productIndex].quantity >= productObj.quantity) {
inventory[productIndex].quantity -= productObj.quantity;
console.log(`Remaining ${productName} pieces: ${inventory[productIndex].quantity}`);
}
else {
console.log(`Not enough ${productName} available, remaining pieces: ${inventory[productIndex].quantity}`);
}
if (inventory[productIndex].quantity === 0) {
inventory.splice(inventory[productIndex], 1);
}
}
else {
console.log(`${productName} not found`);
}
};
//Test 8: flour quantity updated
//Test 10: flour added to inventory
//Test 12: flour not found
//Test 13&14: Remaining flour pieces: 15
// [{name: 'flower', quantity: 15}, {name: 'rice', quantity: 5}]
//Test 15: Remaining flour pieces: 0
// [{name: 'rice', quantity: 5}]
//Test 16: Not enough flour available, remaining pieces: 5
// [{name: 'flour', quantity: 5}, {name: 'rice', quantity: 5}]
I was able to adjust my removeProduct function to align with the required parameters of the tests. Additionally, after searching for a solution, I found the method I used for my findProductIndex function was not the most efficient. After changing my first function to the more efficient method, and fixing my third function, I have passed all the tests. However, I still do not understand exactly why my first function was the problem. The original funciton I wrote passed its own tests and as far as I can tell gives the same output as the more efficient version.