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 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

}
console.log(product);
// 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 :)