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

Student Search Challenge Extra Credit Solution

This is my solution to the extra credit for the Student Search challenge (display information for multiple entries and display error message for search not found). I would love any feedback or suggestions for improvement.

/* Variables */
var search;
var student;
var message = '';
var matches = [];

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

// Creates message containing student record for variable student defined in for loop
function studentRecord(student) {
  var report = '<h2>Name: ' + student.name + '</h2>';
  report += '<h3>Track: ' + student.track + '</h3>';
  report += '<h3>Achievements : ' + student.achievements + '</h3>';
  report += '<h3>Points : ' + student.points + '</h3>';
  return report;
}

/* Report Program */

// while loop runs until user quits

while (true) {
  search = prompt('Enter student\'s name to search records, or type \'quit\' to exit search. Once you have entered a student\'s name, enter \'quit\' to view results');
  if (search === null || search === 'quit') {
    break;
  } else {  
      for ( var i = 0; i < students.length; i += 1) {
        student = students[i];
        if ( search != student.name ) {
          message = 'Sorry. We couldn\'t find that student. Refresh the page to try another search.';
          print(message);
        } else {
    // for loop assigns each object i to variable student, checks for match between student and search, and if match, calls studentRecord function (student) to print(message)
          for ( var i = 0; i < students.length; i += 1) {
            student = students[i];
            if ( search === student.name ) {
              message = studentRecord(student);
              matches.push(message);
              print(matches.join(' '));
            }    
          }
        }
      }  
  } 
} ```