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

Saqib Ishfaq
Saqib Ishfaq
13,912 Points

has any1 completed the extra challenge for the final student record project solution?

any tips or hints plz as i am scratching my head over it for hours:(

2 Answers

function print(message) {
  document.write(message);
  /*var div = document.getElementById("output");
  div.innerHTML = message;*/
}

switching the print function back to document.write(message); displays all records with same name; it's the only solution I've come up with yet.

This is probably haphazard but I was in a hurry. It could be better.

/*
  student_report.js
  Description:
    Prompt renders asking user to input a name or quit
      -if name is found:
         .Display report:
          Name - bold print
          Track
          Points
          Achievements
      -if name not found
        .Display error message
     -if quit - exit out of program - break
     -if list - just do a listing

   Search criteria: 
    -name - search for name - 
      . allow for duplicate names

   Report criteria
    -headers first letter captilized


*/

var message = '';
var lines = "";


/*
  function: getNameReport
  Purpose: returns formatted HTML based on certain names
  parameter: string, object
  returns string
*/
function getNameReport( name, students ){
  var lines = "";

  var sameNames = students.filter ( function ( student ) {
    return student.name === name;
  });


  lines = generateReport( sameNames );

  return lines;

}

/*
  function: generateReport
  Purpose: Generate Report and return formatted html string
  Parameter: Objeft
  Returns: String
*/
function generateReport( studentObj ){
  var lines = "";
  for ( var i = 0; i < studentObj.length ; i++ ){
    lines += displayReportLines( studentObj[i] );  
  }

  return lines;
}


/*
  function: displayReportLines
  Purpose: Returns a line of formatted HTML
  Parameter: object
  Returns: String;
*/
function displayReportLines ( line ) {
  var reportLine = "";

  reportLine += "<h2>Student: " + line.name + "</h2>";
  reportLine += "<p>Track: " + line.track + "</p>";
  reportLine += "<p>Achievements: " + line.achievements + "</p>";
  reportLine += "<p>Points: " + line.points + "</p>";


  return reportLine;
}

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


while( true ) {
  var usrInput = prompt("Enter Student Name[Ex. Jody] or [LIST] for a listing of students or [QUIT] to quit");
  if( usrInput === null || usrInput.toUpperCase() === "QUIT" ) {
    break;
  } else if( usrInput.toUpperCase() === "LIST" ){
    console.log ( usrInput );
    message = generateReport( students );
  } else {
    // its neither a quit or list
    message = getNameReport ( usrInput, students);
    if ( message === "" ) {
      message = "<h2> No name found! </h2>";
    }

  }

}

print(message);