Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Modify Object Properties

what is wrong here?

let product={
   name: "C&C",
   inventory: 1,
   unit_price: 16644.66

function addInventory (object, number) {
  object.inventory += number;
   console.log( number+ " "+ object.name+ "is added to inventory");

addInventory(chair, 3);

i get an error that says" ReferenceError: chair is not defined "

What am I doing wrong here?

2 Answers

Jesus Mendoza
Jesus Mendoza
23,275 Points

Hi Gözde,

The problem is that chair must be a string, otherwise, JavaScript thinks you are referring a variable that does not exist. Thats the reason you are getting a "Reference Error", you can't reference a variable that does not exist.


addInventory("chair", 3);

Wait a minute here. Doesn't this function take in an object name and then a number value? You should be adding to the object inventory with this code:

object.inventory += number;

It should be:

addInventory(product, 3);

where the first argument is the object product.

I think you have this confused and are trying to add a new property or change the property 'name' value to chair.