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

isral duke
isral duke
7,315 Points

Request for Improvement ideas

i took on Dave's challenge to improve the JS code to search through a manifest of students. Even if there are multiple students with the same name it works. i also added a bit to display a message when there isn't a match between the search and the manifest.

Anyone care to offer tips on improving it?

var message = '', search, student, resultsArray = [], result, report;
function print( report ) {
    var outputDiv = document.getElementById('output');
    outputDiv.innerHTML = report ;
}
function getStudentReport( resultsArray ) {
    report = '';
    for ( var j = 0; j < resultsArray.length; j += 1) {
        var item = resultsArray[j];
        report     += '<h2>Student: '     + item.name + '</h2>';
        report     += '<p>Points: '       + item.points + '</p>';
        report     += '<p>Track: '        + item.track + '</p>';
        report     += '<p>Achievements: ' + item.achievements + '</p>';
    }
    print( report );
}
while ( true ) {
    // reset resultsArray before loop restarts
    resultsArray = [];
    search = prompt('Search student records for a name or "quit" ');
    if ( search === null || search === '' || search.toLowerCase() === 'quit' ) {
        break;
    }
    else {
        for ( var i = 0; i < students.length; i += 1 ) {
            student = students[i];
            if ( student.name.toLowerCase() === search.toLowerCase() ) {
                resultsArray.push( student );
            }
        }
        if ( resultsArray.length === 0 ) {
            print('<h2>No results found.</h2><p>Try again.</p>')
        }
        else {
            getStudentReport( resultsArray );
        }
    }
}