If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Get Help > Ask for Help button located on the challenge.
The Ask for Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.
Thank you.
Hi, Im having trouble trying to get the tests 12, 14, 16 to pass - any help would be great - thanx.
This is the only way I can post my code because for me (I dont know if its a site-wide issue) I cant use the code icon in the posting element because the site freezes and I have no use of the cursor to anything further.
const inventory = \[\]
function findProductIndex(productName) {
// convert product name to lowercase
const searchTerm = productName.toLowerCase()
const index = inventory.findIndex(
item => item.name === searchTerm);
console.log("index = ", index)
return index
}
function addProduct(product) {
// convert product name to lowercase
product.name = product.name.toLowerCase()
const existingItem = inventory.find(
item => item.name === product.name);
// if present...
if (existingItem) {
existingItem.quantity += product.quantity
console.log(\`${product.name} quantity updated\`)
} else {
// if NOT present...
// push product to inv arr
inventory.push(product)
console.log(\`${product.name} added to inventory\`)
}
}
function removeProduct(product, amount) {
// convert product name to lowercase
const searchTerm = product.toLowerCase();
// 1. Find the index (returns -1 if not found)
const index = findProductIndex(searchTerm)
// 2. Not found check
if (index === -1) {
console.log(\`${searchTerm} not found\`);
return; // Exit early
}
const existingItem = inventory\[index\];
// 3. Check if we have enough stock BEFORE subtracting
if (amount > existingItem.quantity) {
console.log(\`Not enough ${existingItem.name} available. Remaining pieces: ${existingItem.quantity}\`);
// Optional: Stop here so the quantity doesn't become negative
return;
}
// 4. Subtract the amount
existingItem.quantity -= amount;
console.log(\`Remaining ${existingItem.name} pieces: ${existingItem.quantity}\`);
// 5. Remove if empty
if (existingItem.quantity === 0) {
inventory.splice(index, 1);
console.log(\`${searchTerm} removed from inventory.\`);
}
console.log(inventory);
}
findProductIndex("Bread")
removeProduct("Bread", 5)
// addProduct({ name: "flour", quantity: 20 })
// addProduct({ name: "flour", quantity: 5 })
// addProduct({ name: "rice", quantity: 5 })
console.log("\\n\\n")
// removeProduct("FLOUR", 5)
removeProduct("FLOUR", 10)
also pressing 3 backticks to enter code causes same issues (for me) as I described above.
Hi. I have formatted the code the best I can.
Can you please post the url of the challenge you are on.
Thank you for that - I guess thats answered my question of: Is it just me who is having code posting issues?
The URL you asked for: https://www.freecodecamp.org/learn/javascript-v9/lab-inventory-management-program/build-an-inventory-management-program
btw: how did you format that code?
Hi
I edited the post to add 3 backticks before and after the code. I think you can edit your own posts to do the same
Hi
Can you please clean your code and post it again. There are too many syntax errors, I think because of the escape characters such as the inventory variable.
When you enter a code block into a forum post, please precede it with three backticks to make it easier to read.
You can also use the âpreformatted textâ tool in the editor (</>) to add the backticks.
See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').
Test 12 - when I tested your code it was also printing the index numbr on the screen as well as âflour not foundâ. That isnât asked for. Similar with 14. Your code is also printing out the inventory which isnât asked for.
For 16, the text of your code printed to the console is not exactly the same as asked for.
This is now solved - Thankyou @a1legalfreelance for your help. I can see now that i need to pay more particular attention to the small details (like those extra un-needed console.logs)
Thank you again - you are very helpful ![]()

