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 Create an Object Literal

HELP: object.property returns undefined

Why is my code not updating the value of inventory and why does product.inventory returns undefined when logged to the console:

let product = {
  name : 'Computer',
  inventory : 99,
  unit_price : 399.99

// 2. Create a function named addInventory(). The function should accept 2 parameters -- the product object, and the number to add to the inventory. The function adjusts the product object's inventory property by adding the number passed into the function. 
// For example, if the inventory of the product is currently 2, calling addInventory(product, 3) will set the value of inventory to 5
/* Add a console.log() message to the function that includes a message that looks something like this:
"3 chairs added to the inventory"
In this example, "chair" is the name of the product.
function addInventory(product, number) {
  product.name = product;
  product.inventory += number;
  console.log(number + " " + product['inventory'] + " added to invetory");

// 3. Call the addInventory() function
addInventory('computer', 1);
console.log(product.inventory); // returns 99 NOT 100

1 Answer

Jason Pallone
Jason Pallone
11,340 Points

At 3. You're passing 'computer' as a argument to your function, you want to be passing your variable named, product. By passing 'computer' the function has no access to the product object, so it cannot change its properties.

I hope this helps, feel free to reach out if you need more help :)