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

Luis Villarreal
Luis Villarreal
10,040 Points

Here is my solution to the final challenge. Feel free to comment.

var student = '';
var html = '';
var search;
var message = '';
var MoreThenOneStudentWithSameName = 0;

//Prints out Student Final Result
function print(message){
  var html = document.getElementById('output');
  if(MoreThenOneStudentWithSameName >= 2){
    html.innerHTML += message; //Prints more then one student with    same name
  } else {
    html.innerHTML = message; //prints onely one student
  }
}

//Student Report is Created
function getStudentReport( student){
  var report = '<h2><b>Student: ' + student.name + '</b></h2>';
  report += '<p>Track: ' + student.track + '</p>';
  report += '<p>Achievements: ' + student.achievements + '</p>';
  report += '<p>Points: ' + student.points + '</p>';
  return report;
}

//Main Program
while(true){

  MoreThenOneStudentWithSameName = 0; //Restart counter

  search = prompt('Search Student Recods: type a name [Jody] or       type    "quit" to exit.'); //ask for student data.

  search = search.toLowerCase();

  //End the program if nothing is typed or they write quit. 

  if(search === null || search === 'quit' || search === ''){
    break;
  }

  //Loop to check student records with the name provided

  for(var i = 0; i < students.length; i+=1){
    student = students[i];

    if(student.name.toLowerCase() === search){
      MoreThenOneStudentWithSameName += 1;
      message = getStudentReport(student);
      print(message);
    }

  }
    //Why check for MoreThenOneStudentWithSameName? since, MoreThenOneStudentWithSameName never got     executed we conclude that the loop ended with no result. 

    if(MoreThenOneStudentWithSameName === 0){
      message = (search.charAt(0).toUpperCase() + search.slice(1) + ' is not in our system.');//Capitalize      first letter in the name and display message. 
      print(message);
    }
}
// End of Main Program

My solution is functionally similar, but quicker and dirtier (accomplishes the minimum required functionality):

function studentSearch(){
            var output = "";
            var students = [
                {name: "Dave", track: "Front End Developer", achievements: 158, points: 45532},
                {name: "Cal", track: "Back End Developer", achievements: 188, points: 26432},
                {name: "Jody", track: "Designerr", achievements: 228, points: 9382},
                {name: "Phil", track: "Awesome guy", achievements: 88, points: 9001},
                {name: "Philomena", track: "Legendary boxer", achievements: 666, points: 201843},
                {name: "Cletus", track: "Mind Monger", achievements: 5, points: 8975},
                {name: "Cal", track: "Javascript Evangelist", achievements: 927, points: 91822}
            ];

            var searchKey;
            while (searchKey != "quit"){
                searchKey = prompt('Enter student name or "quit" to exit:').toLowerCase();

                for (var i = 0; i < students.length; i++){
                if (searchKey == (students[i].name).toLowerCase()){
                    output += "<p><strong>Name:</strong> " + students[i].name + "<br><strong>Track:</strong> " + students[i].track + "<br><strong>Achievements:</strong> " + students[i].achievements + "<br><strong>Points:</strong> " + students[i].points + "</p>";
                }
            }
            }
            document.getElementById("dataDiv").innerHTML = output;
        }

One thing to note is that you declared the variable "html" twice in your function. the first as a global string and again inside the nested print function. I'd suggest changing the name of one or the other to minimize confusion and potential errors in the intended functionality of your code.