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
levikordus
4,877 PointsOn last part, not sure what i'm doing wrong. Please help :D
Hello all. I am again stuck on this challenge. I am trying to make it so that the program will print out both students named "Luke". I thought what I programmed would work, but I guess not :P.
Thanks to Jennifer Nordell for all the help in the previous step!
3 Answers
Steven Parker
243,656 PointsOne thing popped out at me at first glance:
I noticed that tempIndex is an array, but you use it as a scalar to subscript your students array.
So, instead of:
message = getStudentReport(students[tempIndex]);
You might want something like:
message = "";
for (x in tempIndex) {
message += getStudentReport(students[tempIndex[x]]);
}
Thomas Nilsen
14,957 Points@stevenpark is right. You can't use an array as key when accessing another array.
Also, here is an alternate solution if you want to search for a student in an array of student-objects:
(This is a functional approach)
var students = [
{
name: 'Luke',
track: 'Web Design',
achievments: 5,
points: 2456
},
{
name: 'Levi',
track: 'Front-End Development',
achievments: 12,
points: 3829
},
{
name: 'Greg',
track: 'Web Design',
achievments: 125,
points: 13456
},
{
name: 'Fred',
track: 'iOS',
achievments: 1,
points: 456
},
{
name: 'Dave',
track: 'Front-End Development',
achievments: 134,
points: 12456
},
{
name: 'Luke',
track: 'Front-End Development',
achievments: 124,
points: 1476
},
];
function find(arr, student) {
arr.filter(obj => obj.name === student).forEach(obj => console.log(obj));
}
/*
This prints:
{ name: 'Luke',
track: 'Web Design',
achievments: 5,
points: 2456 }
{ name: 'Luke',
track: 'Front-End Development',
achievments: 124,
points: 1476 }
*/
find(students, 'Luke');
levikordus
4,877 PointsAlright, i'll take a look, thank you! I actually was working on it a bit after i snap shot my work, so i am going to see if what i am working on is going to work, but i'm sure ill screw it up and have to look at this again :P
levikordus
4,877 PointsThank you all so very much for your answers! I think I figured it out "my way" with Steven's code.
levikordus
4,877 Pointslevikordus
4,877 Pointsalright thank you. I actually knew that line was wrong, but for some reason my brain just couldn't figure out what to do. i think i am going to go over arrays again since they are seemingly hard for me.
levikordus
4,877 Pointslevikordus
4,877 PointsActually, if you feel like it, and for the sake of learning, can you please elaborate on this? I am not sure what you mean by scalar and subscript.
After looking at this a bit, i actually remember trying to add 'i' after tempIndex[] but i never thought of adding another for loop.