Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects The Student Record Search Challenge Solution

Mark Tripney
Mark Tripney
8,662 Points

My solution (including the two extra problems).

let search;
let html;

function print(studentData) {
  document.querySelector('#output').innerHTML = studentData;

function buildOutput(student) {
  html += `<h2>Student: ${student.name}</h2>`;
  html += `<p>Track: ${student.track}</p>`;
  html += `<p>Achievements: ${student.achievements}</p>`;
  html += `<p>Points: ${student.points}</p>`;

function findStudent() {
  // Flag required to prevent 'not on file' message in every loop.
  let notOnFile = true;
  for (let i = 0; i < students.length; i += 1) {
    if (students[i].name.toLowerCase() === search.toLowerCase()) {
      notOnFile = false;
    } else if (notOnFile) {
      const output = `<h2>${search} is not on file.</h2>`;

while (true) {
  search = prompt("Type a name (or 'quit' to close):");
  // null is returned if user presses the prompt's 'Cancel' button.
  if (search.toLowerCase() === 'quit' || search === null) {
  } else {
    // Clear screen before showing next result.
    html = '';

(My students array (const students = [ ... ]), was in an external file, hence the references to students in this script.)

I was working in Firefox Developer Edition, where the output is shown dynamically, behind the prompt box. Therefore, I made the design decision to have the screen clear each time a new search is made. This keeps the output cleaner and more readable. If your browser doesn't work this way, simply remove the html = '' line in the while loop.

function findStudent() uses a flag, notOnFile, to control whether or not the 'not on file' message is shown. This is necessary because, otherwise, this message would be shown for every iteration of the loop which didn't match the search term. In other words, you'd always get the 'not on file' message!

3 Answers

Steven Parker
Steven Parker
210,475 Points

Another approach for the "not on file" message is to wait until the search loop has completely finished before showing it.

That will also be helpful if you want to enhance the code to show the results of multiple searches instead of just the last one done.

Trevor Maltbie
.a{fill-rule:evenodd;}techdegree seal-36
Trevor Maltbie
Full Stack JavaScript Techdegree Graduate 16,996 Points

I don't understand how this shows us all students of the same name. It works, yes, but how? Is it because you made the findStudent() function and added that to the while loop?

Mark Tripney
Mark Tripney
8,662 Points

Hey Trevor. No, the critical work is being done in findStudent(). The while loop is relatively banal — it's handling user input and, when appropriate, asking findStudent() to get to work.

The key is the relationship between findStudent() and buildOutput(), and the for loop in findStudent(). The for loop is iterating over all students and, every time a match is found between the user input and a student's name value, buildOutput() assembles the HTML and asks print() to insert it into the DOM. This process continues so long as there are student records to check — when there are none left unchecked, the for loop is exhausted and the process ends.

Hope this helps. Any questions, gimme a shout.