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

Sascha Heider
Sascha Heider
1,499 Points

Looking for improvementon my extra credit solution

I worked longer than I should have on this solution, anyone got something for me to improve?

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


function getStudent(student)
{
  var oneStudent = '<h2>Student: ' + student.name + '</h2>';
  oneStudent += '<p>Track: ' + student.track + '</p>';
  oneStudent += '<p>Points: ' + student.points + '</p>';
  oneStudent+= '<p>Achievements: ' + student.achievements + '</p>'; 
  return oneStudent;
}

// get information about all students passed to the function
function printStudents(students) {
    var allStudentsFound = "";
    for (var j = 0; j < students.length; j++) {
        allStudentsFound += getStudent(students[j]);
    }
    print(allStudentsFound);
}

// return array 
function findStudents(students, name) {
    var listFound = [];
    for (var i = 0; i < students.length; i++){
        if (name.toLowerCase() === students[i].name.toLowerCase()) {
            listFound.push(students[i]);
        }
    }
    return listFound;
}

while (true) {
    //receive input
    var answer = prompt("Search for student records: type a name [Jody] (or type \"quit\" to end. Type \"list\" for a list of all students.)");

    // break on quit
    if (answer === null || answer.toLowerCase() === "quit") {
      break;
    }


    // find students with given name
    var found = findStudents(students, answer);

    // print students
    if (found.length > 0) {
        printStudents(found);
    } else if (answer.toLowerCase() === "list"){
        printStudents(students);
        break;
    } else {
        print("There are no students named " + answer);
    }
}