Cuéntanos qué está pasando:
Hi everyone,
Issue:
- addProduct({name: “FLOUR”, quantity: 5}) should log “flour quantity updated” when “flour” exists.
- addProduct({name: “FLOUR”, quantity: 5}) should log “flour added to inventory” when “flour” doesn’t exist.
- removeProduct(“FLOUR”, 5) should log “Remaining flour pieces: 15” when inventory = [{name: “flour”, quantity: 20}, {name: “rice”, quantity: 5}].
My code seems correct, but the expected logs don’t match the logic. Any ideas? Thanks!
Tu código hasta el momento
const inventory = [];
function findProductIndex (nombre){
return inventory.findIndex(elem => elem.name === nombre.toLowerCase());
}
function addProduct (objeto){
objeto.name = objeto.name.toLowerCase();
let index = inventory.findIndex(elem => elem.name === objeto.name);
if (index !== -1){
inventory[index].quantity += objeto.quantity;
console.log(`${inventory[index].name} quantity updated`);
} else {
inventory.push(objeto);
console.log(`${objeto.name} added to inventory`)
}
}
function removeProduct(nombre, cantidad){
let index = inventory.findIndex(elem => elem.name === nombre.toLowerCase());
if(index !== -1){
if (inventory[index].quantity > cantidad){
inventory[index].quantity -= cantidad;
console.log(`Remaining ${inventory[index].name} piece: ${inventory[index].quantity}`)
} else if (inventory[index].quantity === cantidad){
inventory.splice(index, 1);
} else if (inventory[index].quantity < cantidad){
console.log(`Not enough ${inventory[index].name} available, remaining pieces: ${inventory[index].quantity}`)
}
} else {
console.log(`${nombre.toLowerCase()} not found`);
}
}
Información de tu navegador:
El agente de usuario es: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Información del Desafío:
Build an Inventory Management Program - Build an Inventory Management Program