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

Michel Ribbens
Michel Ribbens
18,417 Points

my code: any recommendations to improve it?

I've been trying to get a bit more used to ES6 so I did a few exercises by coding this way. for example, here is my code for this exercise and added a couple of things to test things out.

please let me know what you think of it and how I can improve it:

const product = {
  name: 'chair',
  inventory: 20,
  unit_price: 45.99
};

addInventory = (prod, add) => {
  prod.inventory += add;
  console.log(`${add} ${prod.name}s added to the inventory.`);
  console.log(`The inventory now has ${prod.inventory} ${prod.name}s in stock.`);
}

addInventory(product, 8);

processSale = (prod, sold) => {
  prod.inventory -= sold;
  let sales = sold * prod.unit_price;
  let totalValue = prod.inventory * prod.unit_price;
  console.log(`${sold} ${prod.name}s sold for an ammount of $${sales.toFixed(2)}`);
  console.log(`The inventory now has ${prod.inventory} ${prod.name}s in stock with a total value of $${totalValue.toFixed(2)}.`);
}
processSale(product, 6);
Dave StSomeWhere
Dave StSomeWhere
19,870 Points

It might be useful to update the actual product inventory with adds or sales. The messaging to the console could be DRYed into a function. Naming the Chair as product doesn't seem to allow for multiple products. Watch out for having lines to long and needing to scroll to see the code - could be formatted prettier.

Just a few thoughts - good to see you taking concepts to the next level and sharing. :thumbsup: thanks

1 Answer

Michel Ribbens
Michel Ribbens
18,417 Points

thanks Dave StSomeWhere ! Totally agree with your tips, will update the code in the weekend