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 JavaScript Loops, Arrays and Objects Tracking Data Using Objects The Student Record Search Challenge Solution

PLease help i cannot understand some code , how it works

var message = '';
var student;
var search;

function print(message) {
  var outputDiv = document.getElementById('output');
  outputDiv.innerHTML = message;
}
while(true) {
search = prompt('please type a students name');
if(search === null || search.toLowerCase() === 'quit' ) {
break;
}
for (var i = 0; i < students.length; i += 1) {
  student = students[i];
  if(student.name === search) { // some code will run)

  }
  message += '<h2>Student: ' + student.name + '</h2>';
  message += '<p>Track: ' + student.track + '</p>';
  message += '<p>Points: ' + student.points + '</p>';
  message += '<p>Achievements: ' + student.achievements + '</p>';
}


}

consider the instructor code for this challenge solution and in this the for loop and the if statement student.name === search it means if search input is equal to student in that array then some code runs but how is it possible as when the code runs value of i is 0 so the if statement would become students[0].name this is equal to first name of student not all of them so if i write another name of student in the input that is in the array but the index is not 0 this code will not run but the instructor code runs plz help me to explain this code .

5 Answers

jonathan chadeyras
jonathan chadeyras
15,015 Points

Hello,

You only print one student name at the time.

In this example 'students' (with a s at the end) is an Array of Object. In the for loop you first store the value of the array 'students' at the index 'i' in a variable called 'student' (without the s): student = students [i]

The variable 'student' is now an object.

On the next line: if (student. name === search) {// do some code}, you try to access the 'name' property of the newly created 'student' object and see if it matches the 'search' variable. If it does, some code is executed, else nothing happens.

Hope this has been helpful, let me know if you need more explanation.

what iam trying to say is that when the code is executed the value of i is 0 then the code student = students[i] will be student = student[0 ] which is equal to student[0].name which is equal to dave so it returns only first name not all so how can we search for all names

jonathan chadeyras
jonathan chadeyras
15,015 Points

Sorry i'm having a hard time understanding what you try to achieve! my bad.

Are you trying to get one specific value out or are you trying to get several values out, or print all the array or do you get a value that you don't want?

what iam trying to say is that i want to understand the functioning of

for (var i = 0; i < students.length; i += 1) {
  student = students[i];
  if(student.name === search) { // some code will run)

  }
  message += '<h2>Student: ' + student.name + '</h2>';
  message += '<p>Track: ' + student.track + '</p>';
  message += '<p>Points: ' + student.points + '</p>';
  message += '<p>Achievements: ' + student.achievements + '</p>';
}


}
jonathan chadeyras
jonathan chadeyras
15,015 Points

The brute code there would do that:

Loop over students. Store students[i] in a variable student, if student.name match search { nothing since there is no code }

then update the 'message' variable with the new student.name, .track, .points, and .achievements whether or not the student.name matches the search.

I re-checked Dave video and at the stage you're showing, the code is not 'ready' yet, later in the video he creates a function called 'getStudentReport' that takes a student object as a parameter and return the formated 'message' variable and calls this function from inside the 'if' statement.

thank u

jonathan chadeyras
jonathan chadeyras
15,015 Points

You're welcome,

Please mark your question as answered so it can help others.

Have fun coding. :)