Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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,288 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.

Solution:

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.