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

Brayden Gosse
Brayden Gosse
2,280 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' ]
0

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
216,891 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.