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

I did it!!! My solutions... Do you like them?

var input;
var student;
var studentName;
var message = '';
var noStudent = '';
var counter = 0; 
var i = 0;

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

while (true) {
    input = prompt('Type the name of student or type \"quit\" to cancel');
    counter = 0;
    i = 0; 
    if ( input.toUpperCase() === 'QUIT' || input === null ) {
        break;
        }
    while ( i < students.length ) {
        student = students[i];
        studentName = student.name;
        if ( input.toUpperCase() === studentName.toUpperCase() ){
            for ( var key in student ) {
                if ( key === 'name' ) {
                    message += '<h2>Student: ' + studentName + '</h2>';
                } else {
                message += '<p>'+ key + ': ' + student[key];
                }
            }

        } else {
            counter += 1;
            if ( counter === students.length ) {
                message += '<h2>No student with the name ' + input + '</h2>';
            }
        }

        print(message);
        i += 1;
    }
}

2 Answers

The code is difficult to read, as it is very deeply nested.

I recommend splitting the code into different functions.

Good reminder Alex. Clearly named functions that describe their purposes is good practice.

You should switch around the order of: if ( input.toUpperCase() === 'QUIT' || input === null ). It should be: if ( input === null || input.toUpperCase() === 'QUIT' ). I'm getting this error message when I run your code in the browser: Uncaught TypeError: Cannot read property 'toUpperCase' of null.