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

How does the code return the data of one student?

I am bit confused as to how the code works.

Dave says if the user types a name that matches the name property in one of our student objects, then the function runs and the data of that student is shown.

Could someone explain how the code returns the data of the student that has been searched for?

if (student.name === search) {
  message= getStudentReport ( student );
print(message);
} 

Which video are you referring to?

2 Answers

I think that Colin is referring to the last challenge in the Javascript Loops, Arrays, and Objects course. The if statement shown in his question is part of a for loop that loops through the students array. The two lines above the if statement read '''

Sorry...didn't mean to hit 'post' yet. The two lines above the if statement show for (var i = 0; i < students.length; I += 1) {student = students[i]. So the variable student refers to the student from the array that you are currently checking, and student.name refers to the name property for that current student. Search is the variable that refers to the name entered by the user for which you are searching. If the current student's name matches the search name, Dave is executing the function getStudentReport to pull the property values (name, track, achievements, points) for the current student, build a string with all of that data, and return that string as the message that is then printed.

Does that help?

Thanks Carolyn, that was helpful. Sorry I wasn't clear which video this referred to!

You're welcome, Colin! I'm glad I could help. I had just finished that video myself so I recognized it. :-)