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!

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 Introducing the Practice

Brayden Gosse
Brayden Gosse
4,707 Points

totalValue function returns 0

I did this slightly differently by nesting the Arrays in an Object but the first function worked fine. However, when I run the code my second function just shows 0 in the console. Any ideas? I have pasted my code and the result I get in the console below.

Result in the console: [ 'cheese', 'milk' ]

Code: let products = { name: ["cheese", "milk"], inventory: [20, 10], unit_price: [10.0, 5.0] };

let listProducts = () => products.name; console.log(listProducts());

let totalValue = (prods) => { let inventory_value = 0; for (let i=0; i<prods.length; i+=1) { inventory_value += prods[i].inventory * prods[i].unit_price; } return inventory_value; } console.log(totalValue(products));

1 Answer

Steven Parker
Steven Parker
228,025 Points

In the video, "projects" is an array of multiple objects. But in this code, "projects" is a single object containing attributes that are arrays.

This unconventional organization could be made to work, but the loop code would need to be significantly modified.