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

Nour El-din El-helw
Nour El-din El-helw
8,241 Points

Can't Find A Way To Solve This

So..i fixed the same name problem but now there is that each time the code in the for loop runs if 'search' doesn't equal the name in the object it displays the warning alert. Here is my code.

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

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 );
    }
    if (student.name !== search) {
      alert("No student with the name " + search + " was found");
    }
  }
  print(message)
}

2 Answers

billythebobbo
billythebobbo
2,622 Points

My answer was very similar to yours. I assigned the variable message to the empty string at the beginning of the while loop, checked if message was empty after iterating through the students array, and if it was I would use the alert function. It should look something like this:

while (true) {
  message = '';
  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);
    }
  }
  if (message === '') {
    alert('No student with the name ' + search + ' was found.');
  }
  print(message)
}

If message was still equal to the empty string after iterating through the students array, that must mean it didn't find a student by that name, and can inform the user of that fact. Hope this helps.

Nour El-din El-helw
Nour El-din El-helw
8,241 Points

Oh I get you that is actually brilliant! Thanks a lot!

Adam Beer
Adam Beer
11,314 Points

What happens if you put the print (message) function inside the for? Like this.

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 );
    }
    if (student.name !== search) {
      alert("No student with the name " + search + " was found");
    }
    print(message)
  }
}