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

Solution for Extra Credit (Student Record Search Challenge) - Feel free to review code and provide feedback!

First response that I've written, wracked my brain for a bit, so pretty proud of my progress. Thanks to those who have a look!

// variables that I used for the program as well as the print function.
var student;
var userInput;
var name;
var message = "";
var counter = 0;
var reports = [];
var reportAvailable = false;

function print(message) {
  var outputDiv = document.getElementById('output');
  outputDiv.innerHTML = message;
}
//formats report from object and returns it
function getReport(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;
}
//resets key variables used for multiple report lookups
function reset() {
  message = "";
  counter = 0;
  reports = [];
  reportAvailable = false; 
}
//stores the report (for potential multiple reports)
function storeReport() {
  reports[counter] = student;
  counter += 1;
}
//retrieves input from user
while (true) {
  userInput = prompt("Please input a name or type quit to exit.");

  if (userInput === null || userInput === "exit") {
    break;
  }
//valid/non-error responses (including "") are passed through
  userInput = userInput.toLowerCase();

  for (var i = 0; i < students.length; i += 1) {
    student = students[i];
    name = student.name.toLowerCase();
//if the user input matches a valid report, the report is stored in a separate array and is flagged to advise that there is a report available

    if (name === userInput ) {      
      storeReport(student);
      reportAvailable = true; 
    }
  }
  // retrieves and displays reports, or advises that there aren't reports to display
  if (reportAvailable) {

    message += reports.length + " report(s) for retrieval.";

    for (var i = 0; i < reports.length; i += 1) {
      var reportRequest = reports[i];
      message += getReport(reportRequest);
      print(message);   
    }   
  } else { 
    print("<p>" + userInput + " could not be found. Please try again!</p>");    
  } 
  reset(); //resets variable for multiple lookups
}   

Nice job!

1 Answer

javascript

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

do{
  var response = prompt("Name?");
  for(var i=0;i<students.length;i++){
    student = students[i];
    if(response === student.name){
      found = true;
      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);
    }
  }
  if(found === false && response !== 'gotcha'){
   alert("There is no such student named "+response+"."); 
  }  
}while(response !== 'gotcha' && response != null)

Is this correct?

Hey Star Lord,

Your code successfully accomplishes the extra credit tasks. Hooray! Keep in mind that it helps to comment code so that others can better parse through your train of thought. Also helps when you revisit code after a while as well!

Jeff