Tell us what’s happening:
My code does what it is supposed to do according to the user stories, but I can’t pass the tests from 8 onwards.
Not sure what the issue is…
Your code so far
let inventory = [];
function findProductIndex(name){
for (let prop in inventory){
let lowerInventory = inventory[prop]['name'].toLowerCase();
let productName = name.toLowerCase();
let propIndex = inventory.indexOf(inventory[prop]);
prop++
if (lowerInventory === productName ){
return inventory.indexOf(inventory[propIndex]);
}
}
return -1;
}
function addProduct(product){
let name = product['name'].toLowerCase();
let quantity = product['quantity'];
let index = findProductIndex(name);
let lowerInventory = inventory[index]['name'].toLowerCase()
if (inventory[0] === undefined || lowerInventory !== name){
inventory.push({name, quantity});
return `${name} added to inventory`;
}
else if (lowerInventory === name){
inventory[index]['quantity'] += quantity;
return `${name} quantity updated`;
}
}
function removeProduct(nameOrg, qty){
let name = nameOrg.toLowerCase();
let index = findProductIndex(name);
if (index === -1){
return `${name} not found`;
}
else {
if (inventory[index]['quantity'] -= qty === 0){
delete inventory[index]['quantity']
return `${name} removed`
}
else if (inventory[index]['quantity'] -= qty <=0){
return `Not enough ${name} available, remaining pieces: ${qty}`
}
else {
inventory[index]['quantity'] -= qty
return `Remaining ${name} pieces: ${qty} quantity updated`;
}
}
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36
Challenge Information:
Build an Inventory Management Program - Build an Inventory Management Program
sanity
October 13, 2025, 9:32am
2
Have you tried to check what happens when function is called the way it is in test?
I’ve added addProduct({name: "FLOUR", quantity: 5})
at the bottom of code, and console displays error:
TypeError: Cannot read properties of undefined (reading ‘name’)
jamson
October 13, 2025, 11:12am
3
This type of project is great for learning data handling and state management. Clean handling of names and quantities makes a big difference, especially when scaling inventory or managing offline data.
Yes, that relates to this line: let lowerInventory = inventory[index][‘name’].toLowerCase();
in the add addProduct(product) function. But I don’t understand how else to call the “name“ from the inventory array and set it to lower case.
I have tried changing it to all finds of other options….
sanity
October 13, 2025, 6:09pm
5
But what in situation when there’s no such name in the inventory yet?
let inventory = [];
function findProductIndex(name){
for (let prop in inventory){
let lowerInventory = inventory[prop]['name'].toLowerCase();
let productName = name.toLowerCase();
let propIndex = inventory.indexOf(inventory[prop]);
prop++;
if (lowerInventory === productName ){
return inventory.indexOf(inventory[propIndex]);
}
}
return -1;
}
function addProduct(product){
let name = product['name'].toLowerCase();
let quantity = product['quantity'];
let index = findProductIndex(name);
let indexInventory = inventory[index];
let lowerInventName = indexInventory?.name.toLowerCase();
if (lowerInventName === name){
inventory[index]['quantity'] += quantity;
return `${name} quantity updated`;
}
else if
(inventory[0] === undefined || lowerInventName !== name){
inventory.push({name, quantity});
return `${name} added to inventory`;
}
}
console.log(addProduct({name: "flour", quantity: 5})); // flour added to inventory
console.log(inventory); //[ { name: 'flour', quantity: 5 } ]
function removeProduct(nameOrg, qty){
let name = nameOrg.toLowerCase();
let index = findProductIndex(name);
let indexInventory = inventory[index];
let lowerInventName = indexInventory?.name.toLowerCase();
let newQty = indexInventory?.quantity - qty;
if (lowerInventName !== name){
return `${name} not found`;
}
else {
if (newQty === 0){
delete inventory[index];
return `${name} removed`;
}
else if (newQty <=0){
return `Not enough ${name} available, remaining pieces: ${indexInventory?.quantity}`;
}
else {
inventory[index]['quantity'] = newQty;
return `Remaining ${name} pieces: ${newQty}`;
}
}
}
console.log(removeProduct("FLOUR", 6)) //Not enough flour available, remaining pieces: 5
console.log(inventory) // [ { name: 'flour', quantity: 5 } ]
This is my updated code, but still can’t pass tests 8, 10, 12, 14, 15, 16
This one now works with an empty array, no error with ‘name’ anymore
I have made more changes, but the code still not passing tests 8, 10, 12, 14, 15, 16
I don’t understand why it doesn’t pass the test
let inventory = [];
function findProductIndex(name){
for (let prop in inventory){
let lowerInventory = inventory[prop]['name'].toLowerCase();
let productName = name.toLowerCase();
let propIndex = inventory.indexOf(inventory[prop]);
prop++;
if (lowerInventory === productName ){
return inventory.indexOf(inventory[propIndex]);
}
}
return -1;
}
function addProduct(product){
let name = product['name'].toLowerCase();
let quantity = product['quantity'];
let index = findProductIndex(name);
let indexInventory = inventory[index];
let lowerInventName = indexInventory?.name.toLowerCase();
if (lowerInventName === name){
inventory[index]['quantity'] += quantity;
return `${name} quantity updated`;
}
else if
(inventory[0] === undefined || lowerInventName !== name){
inventory.push({name, quantity});
return `${name} added to inventory`;
}
}
console.log(addProduct({name: "flour", quantity: 5})); // flour added to inventory
console.log(inventory); // [ { name: 'flour', quantity: 5 } ]
function removeProduct(nameOrg, qty){
let name = nameOrg.toLowerCase();
let index = findProductIndex(name);
let indexInventory = inventory[index];
let lowerInventName = indexInventory?.name.toLowerCase();
let newQty = indexInventory?.quantity - qty;
if (lowerInventName !== name){
return `${name} not found`;
}
else {
if (newQty === 0){
delete inventory[index];
return `Remaining ${name} pieces: ${newQty}`;
}
else if (newQty <=0){
return `Not enough ${name} available, remaining pieces: ${indexInventory?.quantity}`;
}
else {
inventory[index]['quantity'] = newQty;
return `Remaining ${name} pieces: ${newQty}`;
}
}
}
console.log(removeProduct("Flour", 5)) // Remaining flour pieces: 0
console.log(inventory) // [ ]
sanity
October 13, 2025, 7:33pm
10
Could you point to the parts responsible for logging text expected by tests?
The logging text expected by the tests is all in the return sections. I have added a comment next to each of the places in the code below
let inventory = [];
function findProductIndex(name){
for (let prop in inventory){
let lowerInventory = inventory[prop]['name'].toLowerCase();
let productName = name.toLowerCase();
let propIndex = inventory.indexOf(inventory[prop]);
prop++;
if (lowerInventory === productName ){
return inventory.indexOf(inventory[propIndex]);
}
}
return -1;
}
function addProduct(product){
let name = product['name'].toLowerCase();
let quantity = product['quantity'];
let index = findProductIndex(name);
let indexInventory = inventory[index];
let lowerInventName = indexInventory?.name.toLowerCase();
if (lowerInventName === name){
inventory[index]['quantity'] += quantity;
return `${name} quantity updated`; //text here
}
else if
(inventory[0] === undefined || lowerInventName !== name){
inventory.push({name, quantity});
return `${name} added to inventory`; //text here
}
}
console.log(addProduct({name: "flour", quantity: 5})); // flour added to inventory
console.log(inventory); // [ { name: 'flour', quantity: 5 } ]
function removeProduct(nameOrg, qty){
let name = nameOrg.toLowerCase();
let index = findProductIndex(name);
let indexInventory = inventory[index];
let lowerInventName = indexInventory?.name.toLowerCase();
let newQty = indexInventory?.quantity - qty;
if (lowerInventName !== name){
return `${name} not found`; //text here
}
else {
if (newQty === 0){
delete inventory[index];
return `Remaining ${name} pieces: ${newQty}`; //text here
}
else if (newQty <=0){
return `Not enough ${name} available, remaining pieces: ${indexInventory?.quantity}`; //text here
}
else {
inventory[index]['quantity'] = newQty;
return `Remaining ${name} pieces: ${newQty}`; //text here
}
}
}
console.log(removeProduct("Flour", 5)) // Remaining flour pieces: 0
console.log(inventory) // [ ]
ILM
October 14, 2025, 7:10pm
12
if the tests expect a certain text to be logged, returning it will not satisfy the tests
make sure you understand the difference between logging a value and returning a value
Thank you @sanity and @ILM for your help! Passed the tests!