This is the code I have written. I want the listContacts function to loop through the contacts array and log the first and last name of each contact to the console. When I run this though it only logs John Doe to the console. What am I missing here?
var contacts = [
{
firstName : 'John',
lastName : 'Doe',
phone : '(512) 355-0453',
email : '[email protected]'
},
{
firstName : 'Jane',
lastName : 'Doe',
phone : '(313) 641-2203',
email : '[email protected]'
},
{
firstName : 'Suzie',
lastName : 'Smith',
phone : '(415) 604-4219',
email : '[email protected]'
}
];
var listContacts = function () {
for (var i = 0; i <= contacts.length; i++) {
return contacts[i].firstName + ' ' + contacts[i].lastName;
}
};
console.log(listContacts());
returnin the first iteration of your loop, as that way you never get any other iterations of that loop.i < contacts.length, not<=.for (let c of contacts) console.log( c.firstName + ' ' + c.lastName );console.log(contacts.reduce( (p,e) => { return p+e.firstName+' '+e.lastName+"\n" }, "") );