yes i try but iam still not good with writing good comments although you gave me some tips to work with but iwas confused cuz i wasn’t able to pass the test so i felt despair somehow , you see how i was testing the function with object as arguments what a big mistaaaaaaaaaake from me , damn
here is another problem i meet here
i deleted the project then started it from the beginning
when i try to work with product name i modify it in each function to be sure it’s lower case
but i meet a problem here
let inventory = [
{name: 'pasta', quantity: 50},
{name: 'flour', quantity: 50},
]
function findProductIndex(productName) {
productName = productName.toLowerCase()
for(let i = 0; i < inventory.length; i++) {
if(inventory[i].name == productName) {
return i
}
}
return -1
}
function addProduct(obj) {
obj.name = obj.name.toLowerCase()
let productIdx = findProductIndex(obj.name);
// check if the product is in the inventory
if(productIdx != -1) {
inventory[productIdx].quantity += obj.quantity
console.log(`${obj.name} quantity updated`)
}
if(productIdx == -1) {
inventory.push(obj)
console.log(`${obj.name} added to inventory`)
}
return obj
}
function removeProduct(productName, quantity) {
productName = productName.toLowerCase()
let productIdx = findProductIndex(productName);
// check if the product is in the inventory
if(productIdx != -1) {
// if so, check if the quantity is enough to subtract
if(inventory[productIdx].quantity > quantity) {
// if yes, subtract the order quantity from the inventory quantity
inventory[productIdx].quantity -= quantity
// log the remaining inventory quantity
console.log(`Remaining ${productName} pieces: ${inventory[productIdx].quantity}`)
}
// if(inventory[productIdx].quantity == quantity) {
// inventory[productIdx].quantity -= quantity
// console.log(`Not enough ${productName} available, remaining pieces: ${inventory[productIdx].quantity}`)
// }
}
}
addProduct({name: 'chocolate', quantity: 150})
removeProduct({name: 'flouR', quantity: 20})
console.log(removeProduct("FLOUR", 30))
console.log(inventory)
what problem have you met?
it says productName.toLowerCase() is not a function ?!!
limme see after i asked for help i found the problem dissappeared
here is the problem shown again
TypeError: productName.toLowerCase is not a function
i resolved it , i used String constructor to be sure that the value is string
here iam , i passed test number 12 finally , but i can’t pass test number 13 although i get results on the console , it says
13. removeProduct("FLOUR", 5)
should subtract 5
from the quantity
value of the object having name
equal to "flour"
inside the inventory
array.
here is my code
…
let inventory = [
{name: 'pasta', quantity: 50},
{name: 'flour', quantity: 50},
]
function findProductIndex(productName) {
productName = productName.toLowerCase()
for(let i = 0; i < inventory.length; i++) {
if(inventory[i].name == productName) {
return i
}
}
return -1
}
function addProduct(obj) {
obj.name = obj.name.toLowerCase()
let productIdx = findProductIndex(obj.name);
// check if the product is in the inventory
if(productIdx != -1) {
inventory[productIdx].quantity += obj.quantity
console.log(`${obj.name} quantity updated`)
}
if(productIdx == -1) {
inventory.push(obj)
console.log(`${obj.name} added to inventory`)
}
return obj
}
function removeProduct(productName, quantity) {
productName = productName.toLowerCase()
let productIdx = findProductIndex(productName);
// check if the product is in the inventory
if(productIdx != -1) {
// if so, check if the quantity is enough to subtract
if(inventory[productIdx].quantity > quantity) {
// if yes, subtract the order quantity from the inventory quantity
inventory[productIdx].quantity -= quantity
// log the remaining inventory quantity
console.log(`Remaining ${productName} pieces: ${inventory[productIdx].quantity}`)
}
if(inventory[productIdx].quantity == quantity) {
inventory[productIdx].quantity -= quantity
console.log(`Not enough ${productName} available, remaining pieces: ${inventory[productIdx].quantity} `)
}
}
if(findProductIndex(productName) == -1) {
console.log(`${productName} not found`)
}
}
addProduct({name: 'ChoCoLaTe', quantity: 150})
removeProduct("FLOUR", 4)
removeProduct("CrispY", 40)
removeProduct("pasta", 20)
console.log(removeProduct("FLOUR", 10))
console.log(inventory)
can you explain this part please?
why are you subtracting and then saying that there is not enough product available?
because user story number 8 said so
If the quantity after the subtraction is zero, removeProduct
should remove the product object from the inventory. If the quantity in the inventory is not enough to perform the subtraction, the removeProduct
function should log the string Not enough <product-name> available, remaining pieces: <product-quantity>
to the console.
ok i deleted it then i passed the test , thanks for your tips
btw, what you see , am i walking on the right way to build a good logic or i fall in a deep hall
you need to try to think more about what you are writing and what you need to code. You arrive at the point yourself with some pointers, so you are on the way to manage on your own eventually
But don’t worry, people always need to ask for help
finally, i finished the inventory challenge, but this time is different , i did not feel happy after it for first time , and when i finished i did not run to pass the challenge i tried some test and i found issue again but i succeed resolved it at the end with small code manipulation ,
but you know , you words encourage me more than the challenge .
yes you right i got some problems in thinking even in my real life
maybe day after day i will be better