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 trialNuradin Sheikh
6,211 PointsI'm not sure where I'm going wrong in my for loop in JavaScript?
I am working on bringing back an array that contains names. I want to bring back the array with all the names except Samir. Can someone point me in the right direction where I'm going wrong? I'm sure it's obvious but I can't seem to see where.
const users = [ {name: 'Samir'}, {name: 'Angela'}, {name: 'Beatrice'} ];
for( i = 0; i < users.length; i++){ if(users.name !== 'Samir'){
console.log(users[i]); } }
1 Answer
Jamie Reardon
Treehouse Project ReviewerYou need to add your index variable to the if condition as well:
if(users[i].name !== 'Samir')
Nuradin Sheikh
6,211 PointsNuradin Sheikh
6,211 PointsThank you!!
Jamie Reardon
Treehouse Project ReviewerJamie Reardon
Treehouse Project ReviewerYou're welcome, just remember these concepts:
users.name
, this wasn't working because users is an array of objects.[0]
which accesses the first item in the array as they are zero-indexed.users[i].name