Tell us what’s happening:
Hey,there. Here is my code, which works in console, but I can’t pass tests 8, 9, 12, 13, 14, 16
I can’t understand why everyting works.For example, when I add values to the inventory, console shows the correct answers
Your code so far
let inventory = []
function findProductIndex(productname){
productname = productname.toLowerCase()
for(let object of inventory){
if(object.name == productname){
return inventory.indexOf(object)
}
}return -1
}
function addProduct(productObject){
for(let object of inventory){
if(object.name == productObject.name.toLowerCase()){
object.quantity = object.quantity + productObject.quantity
return `${object.name} quantity updated`
}
}if (inventory.includes(productObject)== false){
productObject.name = productObject.name.toLowerCase()
inventory.push(productObject)
return `${productObject.name} added to inventory`
}
}
function removeProduct(productName, quantity){
productName = productName.toLowerCase()
for(let object of inventory){
if(object.name == productName){
let newQuantity = object.quantity - quantity
if(newQuantity > 0){
return `Remaining ${object.name} pieces: ${newQuantity}`
}else if( newQuantity === 0){
let index=findProductIndex(productName)
inventory.splice(index,1)
console.log(inventory)
}else if(newQuantity < 0){
return `Not enough ${object.name} available, remaining pieces: ${object.quantity}`
}
}
}
return`${productName} not found`
}
console.log(inventory)
console.log(removeProduct("FLOUR", 5))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36
Challenge Information:
Build an Inventory Management Program - Build an Inventory Management Program
