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

Cody Selman
Cody Selman
7,643 Points

My Solution to "The Student Record Challenge."

Here's a snapshot of my solution: https://w.trhou.se/9ugs9i924e

Here is the relevant JavaScript:

var message = '';
var student;
var html = '';
var searchQuery = '';
var studentIndex = -1;

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

function getSearchPrompt() {
  return prompt('Type the name of the student whose record you would like to display or type "quit" to exit.');
}

function getStudentIndex(searchName){
    return students.findIndex(
      function test(element){
        return element.name === searchName;
      }
    );
}

function getStudentInfo(query){
  studentIndex = getStudentIndex(query);
  if ( studentIndex >= 0 ) {
    student = students[studentIndex];
    message += '<h2>Student: ' + student.name + '</h2>';
    message += '<p>Track: ' + student.track + '</p>';
    message += '<p>Points: ' + student.points + '</p>';
    message += '<p>Achievements: ' + student.achievements + '</p>'; 
    print(message);
    message = '';
  }
}

function getSearchQueries() {
  searchQuery = getSearchPrompt();
  if ( searchQuery.toLowerCase() !== 'quit' ) {
    getStudentInfo(searchQuery);
    getSearchQueries();
 }
}

getSearchQueries();

Let me know if you have any feedback, questions or comments.

1 Answer