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

Oussama Moulana
Oussama Moulana
2,977 Points

My solution in case the student not found and displaying multiple students with the same name

var message = '';
var student;
var search;
var noStudentFound = false;
var studentFound = false;

function print(message) {
  var outputDiv = document.getElementById('output');
  outputDiv.innerHTML = message;
}

function getStudentReport(student) {
  var report = '<h2>Student: ' + student.name + '</h2>';
  report += '<p>Track: ' + student.track + '</p>';
  report += '<p>Points: ' + student.points + '</p>';
  report += '<p>Achievements: ' + student.achievements + '</p>';
  return report;
}

while (true) {
  search = prompt('Search student records: type a name [Jody] (or type "quit" to end)');
  if (search === null || search.toLowerCase() === 'quit') {
    break;
  } 
  for (var i = 0; i < students.length; i += 1) {
    student = students[i];
    if (student.name === search) {
      message += getStudentReport(student);
      print(message); 
    }
    if (student.name === search) {
    studentFound = true;
    } else {
      noStudentFound = true;
    }
  }
  if (studentFound === true) {
  break;
  }
  if (noStudentFound === true) {
  alert('No student named ' + search + ' was found');
  }
}

4 Answers

Adam Beer
Adam Beer
11,314 Points

What's happend when you delete this section? Anyway, your code looks good.

if (student.name === search) {
    studentFound = true;
    } else {
      noStudentFound = true;
    }
  }
  if (studentFound === true) {
  break;
  }
  if (noStudentFound === true) {
  alert('No student named ' + search + ' was found');
Oussama Moulana
Oussama Moulana
2,977 Points

Thank you for your comment Adam.

If i delete this section the code will keep working without the last part of the exercise wich Dave ask us to solve at 5:59 of the video.

  • The code will not stop when it found the students. (I know this is not required, i added this feature for a better experience)
  • The code will not alert us when the name of the student is not found.
  • The code will display all the students we type before we tape "quit"

can you please explain how the two search values with same name display? thank you

Oussama Moulana
Oussama Moulana
2,977 Points

Hello Saud, I did this exercise few months ago, and i'm realizing how hard it is to back into it XD Could you please show me which part of my code you want me to explain to you and i'll do my best

But in case you wanted to understand how my code can display students with the same name, the answer is on this part :

message += getStudentReport(student);

the "+=" concatenate all the students with the same name.

Please let me know Saud if I answered your question

lol its okay i practiced a lot and finally got my head around basic stuff. i'm at the ajax course now lol. thanks for the reply tho i appreciate it