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 Create an Array of Objects

Gal Parselany
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Gal Parselany
Front End Web Development Techdegree Graduate 19,126 Points

my solution :

let products = [
    {
        name : 'tuna',
        inventory : 4,
        unit_price : 8.60
    },

    {
        name : 'Salmon',
        inventory : 10,
        unit_price : 5.60
    },
];

function listProducts(obj) {
    let objName = [];
    for (i = 0; i < obj.length; i++) {
        let indexName = obj[i].name;
        objName.push(indexName);
    }
    console.log(objName);
    return objName;
}

listProducts(products);

function totalValue(arr) {
    let totalVal = 0;
    for (i = 0; i < arr.length; i++) {
        arrVal = arr[i].inventory * arr[i].unit_price;
        totalVal += arrVal;
    }
    console.log(totalVal);
    return totalVal
}

totalValue(products);
Rich Donnellan
Rich Donnellan
Treehouse Moderator 25,866 Points

Added syntax hinting and removed your duplicate posts.