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

Roshini Thiagarajan
Roshini Thiagarajan
8,941 Points

Here is my solution to the challenge:

var message = ''; var student; var response; var studentList = [];

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

function studentRecords(arr){ var report = ''; for(var i = 0; i < arr.length; i++){ var ind = arr[i]; console.log(ind); console.log(students[ind].name); report += '<h2>Student: ' + students[ind].name + '</h2>'; console.log(students[ind].track); report += '<p>Track: ' + students[ind].track + '</p>'; console.log(students[ind].points); report += '<p>Points: ' + students[ind].points + '</p>'; console.log(students[ind].achievements); report += '<p>Achievements: ' + students[ind].achievements + '</p>'; } console.log(report); return report; }

while(true){ response = prompt('Get details of a student by typing their name ex: [Audrey], enter q to quit');

if(response === null || response.toLowerCase() === 'q'){
    break;
}

//If student exists, return details
//If multiple return multiple
//If not, say does not exist.

response = response.toLowerCase();
for (var ind = 0; ind < students.length; ind += 1) {
    student = students[ind];
    if(response === student.name.toLowerCase()){
        studentList.push(ind);
    }
}

if(studentList.length !== 0){
    console.log(studentList);
    message = studentRecords(studentList);

    console.log(message);
    print(message);
}
else if (response.length > 0){
    message = '<p>' + response + ' not in database.</p>';
    console.log(message);
    print(message);
}

//Empty string and message for next iteration
studentList = [];
message = '';

}