Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects The Student Record Search Challenge

Ermin Bicakcic
Ermin Bicakcic
5,142 Points

cannot get this to work with else statement

So I cannot get this code to work once I add else statement (In case that username is not in database) why is that?

let message = '';
let student;

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

while (true) {
    search = prompt("Search for a student in our database and to quit type in 'quit' to exit").toLowerCase();
    if ( search === 'quit') {
        break;
    } else {
        for ( let i = 0; i < students.length; i++ ) {
            if ( search === students[i].name.toLowerCase() ) {
                let data = students[i];
                message += '<h2>Student: ' + data.name + '</h2>';
                message += '<p>Track: ' + data.track + '</p>';
                message += '<p>Achievement: ' + data.achievements + '</p>';
                message += '<p>Points: ' + data.points + '</p>';
                print(message);
            } else {
                print( search + ' is not in database.'); 
            }
        }
    }
}

3 Answers

The issue is you are looping through the entire students array whether the student is found or not. So unless the last student in the array is entered, in this case Trish, the message will always be the one from the else statement. To fix you could break if a student was found.

for ( let i = 0; i < students.length; i++ ) {
    if ( search === students[i].name.toLowerCase() ) {
        let data = students[i];
        message += '<h2>Student: ' + data.name + '</h2>';
        message += '<p>Track: ' + data.track + '</p>';
        message += '<p>Achievement: ' + data.achievements + '</p>';
        message += '<p>Points: ' + data.points + '</p>';
        print(message);
        break;
    } else {
        print( search + ' is not in database.'); 
    }
}

I added a break, but My program couldn't stop

Bartlomiej Pajak
Bartlomiej Pajak
5,062 Points

Or you could remove print(message) from the loop and add it after the loop like this:

let message = '';
let student;

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

while (true) {
    search = prompt("Search for a student in our database and to quit type in 'quit' to exit").toLowerCase();
    if ( search === 'quit') {
        break;
    } else {
        for ( let i = 0; i < students.length; i++ ) {
            if ( search === students[i].name.toLowerCase() ) {
                let data = students[i];
                message += '<h2>Student: ' + data.name + '</h2>';
                message += '<p>Track: ' + data.track + '</p>';
                message += '<p>Achievement: ' + data.achievements + '</p>';
                message += '<p>Points: ' + data.points + '</p>';

            } else {
                print( search + ' is not in database.'); 
            }
        }
    }
}

print(message);