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

Im just starting my JavaScript adventure, and can't seem to figure out how to print out different outputs for an array.

Hey guys, i've started learning JS, and i'm stuck on an excersice. I'm trying to console.log all items in an array, with one consol.logging as an exception.

let friendList = ["Peter", "Tony", "Sarah", "Chloe", "Jo"];
let notInvited = friendList[1];


for (let i = 0; i < friendList.length; i++){
    console.log ((friendList[i]) + " is invited");
    console.log (notInvited + " is not invited");
} 

3 Answers

trying to get the console log to print out:

Peter is invited / Tony is not invited / Sarah is invited / Chloe is invited / Jo is invited

This works, but not sure it is using the correct method. Would like to know the answer too if there's a better way :)

let friendList = ["Peter", "Tony", "Sarah", "Chloe", "Jo"]; let notInvited = friendList[1];

for (let i = 0; i < friendList.length; i++){ if (i!=1) console.log ((friendList[i]) + " is invited"); }

console.log (notInvited + " is not invited");

Hey! thanks Hazel, I've finally tried this, and found it working!

let friendList = ["Peter", "Tony", "Sarah", "Chloe", "Jo"];

let notInvited = ("Tony");

for (let i = 0; i < friendList.length; i++) {

if (friendList[i] === notInvited) { console.log(friendList[i] + " is not invited"); } else{ console.log((friendList[i]) + " is invited"); }

}

Thanks :) !