Build an Inventory Management Program - Build an Inventory Management Program

Tell us what’s happening:

i cant solve test 7;addproduct thing …but the output is ok

Your code so far

let inventory=[]


function findProductIndex(name){

let low=name.toLowerCase();

let index=inventory.findIndex(obj => obj.name === low);

return index
}
console.log(findProductIndex('flour'));

function addProduct(value){

  let low=value['name'].toLowerCase()

if(low === 'flour'){

return value['quantity'] + ',' + '5'
}
}

console.log(addProduct({name: "FLOUR", quantity: 5}));


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36

Challenge Information:

Build an Inventory Management Program - Build an Inventory Management Program

i really dont understand the question below the addproduct function;..
let inventory=

function findProductIndex(name){

let low=name.toLowerCase();

let index=inventory.findIndex(obj => obj.name === low);

return index
}
console.log(findProductIndex(‘flour’));

function addProduct(value){

let low=value[‘name’].toLowerCase()

if(low === ‘flour’){

value[‘quantity’] + ‘5’;

console.log(flour quantity updated)
}else{

inventory.push(value)

console.log(flour added to inventory)
}
}

console.log(addProduct({name: “FLOUR”, quantity: 5}));

Is the output ok?

How can you check the inventory of flour?

if(low === 'flour'){
  return value['quantity'] + ',' + '5'

It looks like you have hard-coded conditionals or variables that check for specific expected values. That is not solving this problem in the general case. Imagine if you were given different input values. Would your code be able to solve those problems?

To find out more about what hard-coding is or about why it is not suitable for solving coding questions, please read this post: Hard-coding For Beginners

Let us know if you have a question about how to make your code more flexible.

is this better?
let inventory=

function findProductIndex(name){

let low=name.toLowerCase();

let index=inventory.findIndex(obj => obj.name === low);

return index
}
console.log(findProductIndex(‘flour’));

function addProduct(value){

let low=value.name.toLowerCase()

if(findProductIndex(low) !== -1){

inventory.quantity += value.quantity;

console.log(${low} quantity updated)
}else {

inventory.push(value)

console.log(${low} added to inventory)
}
}

console.log(addProduct({name: “FLOUR”, quantity: 5}));

Can you show the value of flour in the inventory?

Here are some basic troubleshooting steps you can follow. Focus on one test at a time:

  • What is the requirement of the first failing test?
  • Double check the related User Story and ensure it’s followed precisely.
  • What line of code is involved?
  • What is the result of the code and does it match the requirement? You can write the value of a variable to the console at that point in the code to check if needed.
  • Is there any other output or messages in the console to follow up on?

If this does not help you solve the problem, please reply with answers to these questions.

i am stuck here;
let inventory =

function findProductIndex(name) {

let low = name.toLowerCase();

let index = inventory.findIndex(obj => obj.name === low);

return index
}
console.log(findProductIndex(‘flour’));

function addProduct(value) {

value.name = value.name.toLowerCase()
let index = findProductIndex(value.name)
if (index !== -1) {

inventory[index].quantity += value.quantity;

console.log(`${value.name} quantity updated`)

} else {
inventory.push(value)

console.log(`${value.name} added to inventory`)

}
}

console.log(addProduct({ name: “FLOUR”, quantity: 5 }));

function removeProduct(proname, quantity) {

proname = proname.toLowerCase()

let index = findProductIndex(proname)
if (index === -1) {

console.log(`${proname} not found`)
return;

}
if (index !== -1) {

inventory[index].quantity -= quantity;

}
if (inventory[index].quantity === 0) {
inventory.pop()
}
}

console.log(removeProduct(“FLOUR”, 5))

i corrected this so im stuck here instead 14 and 16;
let inventory =

function findProductIndex(name) {

let low = name.toLowerCase();

let index = inventory.findIndex(obj => obj.name === low);

return index
}
console.log(findProductIndex(‘flour’));

function addProduct(value) {

value.name = value.name.toLowerCase()

let index = findProductIndex(value.name)

if (index !== -1) {

inventory[index].quantity += value.quantity;

console.log(`${value.name} quantity updated`)

} else {
inventory.push(value)

console.log(`${value.name} added to inventory`)

}
}

console.log(addProduct({ name: “FLOUR”, quantity: 5 }));

function removeProduct(proname, quantity) {

proname = proname.toLowerCase()

let index = findProductIndex(proname)

if (index === -1) {

console.log(`${proname} not found`)
return;

}
if (index !== -1) {

inventory[index].quantity -= quantity;

}
if (inventory[index].quantity === 0) {

inventory.splice(0, 2)

}
if (inventory === { name: “flour”, quantity: 20 }, { name: “rice”, quantity: 5 }) {
console.log(Remaining ${proname} pieces: 15)
}

}

console.log(removeProduct(“FLOUR”, 5))

please try to answer these questions

14 says;
removeProduct("FLOUR", 5) should log Remaining flour pieces: 15 to the console, when inventory is equal to [{name: "flour", quantity: 20}, {name: "rice", quantity: 5}] .
there are two undefineds on the ouput;
i dont know how to start solving question 14;
when inventory is …

which output are you looking at?

You dont need to start answering it yet. Try to find the answers to the above questions.

If you need help getting one of the answers please ask.

output
-1
flour added to inventory
undefined
undefined

as your function does not have a return statement, remove the console.log here abd leave only the function call to removeProduct, it’s only confusing you. When a function does not have a return statement, its output is undefined, but that’s fine, you are not asked to return something here

all good now? i still cant pass 14 & 16

let inventory = [];

function findProductIndex(name) {

  let low = name.toLowerCase();

  let index = inventory.findIndex(obj => obj.name === low);

  return index
}
console.log(findProductIndex('flour'));

function addProduct(value) {

  value.name = value.name.toLowerCase()
  let index = findProductIndex(value.name)
  if (index !== -1) {

    inventory[index].quantity += value.quantity;

    console.log(`${value.name} quantity updated`)
  } else {
    inventory.push(value)

    console.log(`${value.name} added to inventory`)
  }
}

addProduct({ name: "FLOUR", quantity: 5 });

function removeProduct(proname, quantity) {

  proname = proname.toLowerCase()

  let index = findProductIndex(proname)

  if (index === -1) {

    console.log(`${proname} not found`)
    return;
  }
if(inventory[index].quantity < quantity){

console.log(`Not enough ${proname} available, remaining pieces: ${quantity}`)

}

  if(inventory[index].quantity === quantity) {

    inventory.splice(index, 1)

    console.log(quantity)
  }else {

    inventory[index].quantity -= quantity;

    console.log(quantity)
  
  console.log(`Remaining ${proname} pieces: ${inventory[index].quantity}`)
  }

  }

removeProduct('FLOUR', 5);
console.log()

I’ve edited your post to improve the readability of the code. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

doing this to check

addProduct({ name: "FLOUR", quantity: 5 });

function removeProduct(proname, quantity) {

  proname = proname.toLowerCase()

  let index = findProductIndex(proname)

  if (index === -1) {

    console.log(`${proname} not found`)
    return;
  }
  if (inventory[index].quantity < quantity) {

    console.log(`Not enough ${proname} available, remaining pieces: ${quantity}`)

  }

  if (inventory[index].quantity === quantity) {

    inventory.splice(index, 1)

  } else {

    inventory[index].quantity -= quantity;


    console.log(`Remaining ${proname} pieces: ${inventory[index].quantity}`)
  }

}

console.log("removing FLOUR")
removeProduct('FLOUR', 5);
console.log("done")

nothign is getting printed.

Which is the part of the function that deals with removing the exact content of the product? is there a console.log there? what would you need to log in that case?

if (inventory[index].quantity === quantity) {

    inventory.splice(index, 1)

the question does'nt require any console.log output
addProduct({ name: "FLOUR", quantity: 5 });

function removeProduct(proname, quantity) {

  proname = proname.toLowerCase()
  let index = findProductIndex(proname)
  if (index === -1) {
    console.log(`${proname} not found`)
    return;
  }
  if (inventory[index].quantity < quantity) {
    console.log(`Not enough ${proname} available, remaining pieces: ${quantity}`)

  }

  if (inventory[index].quantity === quantity) {
    inventory.splice(index, 1)

  } else {
    inventory[index].quantity -= quantity;
    console.log(quantity)

    console.log(`Remaining ${proname} pieces: ${inventory[index].quantity}`)
  }

}

removeProduct('FLOUR', 5);
console.log()


it always require a console.log output