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 Solution

Matt Topping
Matt Topping
4,527 Points

Finding if student exist

Hey all, having trouble getting a proper return for students not found. Because of the for loop it will run the not found code prior to returning student data. is there a way that indexOf() could fix this?

thank you

var html = '<ul style="list-style-type:none">';
var student;
var search;
var message = '';
var innerDiv = document.getElementById("output");

function isaStudent (search){
    for (var i = 0; i < students.length; i++) { 
        if (search === students[i].name) { 
            return true;
        } else {
            return false;
        }
    }
}

function print(message) {
    var text = document.getElementById("output");
    text.innerHTML += message;
}
function getStudentReport(student){
    var html = '<h2>Student: ' + student.name +'</h2>';
    html += '<p>Track: ' + student.track +'</p>';
    html += '<p>Achievements: ' + student.achievements +'</p>';
    html += '<p>Points: ' + student.points +'</p>';
    return html;
}

while (true) {
    search = prompt('Find a student by first name [Kai,Nathan, etc.] "Quit" to exit');
    innerDiv.innerHTML = '';
    if (search === null || search.toUpperCase() === 'QUIT'){
        print('<h1>Goodbye</h1>');
        break;
    } else if (isaStudent(search) != true){
        alert('Sorry, ' + search + ' is not enrolled here.');
        }
for (var i = 0; i < students.length; i++) {
    student = students[i];
 if (student.name === search) {
    message = getStudentReport(student);
    print(message);
    }
  }
}
//console.log(isaStudent(students[0].name));```

1 Answer

Steven Parker
Steven Parker
221,323 Points

:point_right: You don't want your search to give up on the first test.

Wait until the loop has finished checking all students before returning false:

function isaStudent (search){
    for (var i = 0; i < students.length; i++) { 
        if (search === students[i].name) { 
            return true;
        }
    }
    return false;
}
Matt Topping
Matt Topping
4,527 Points

Woah, smooth as butter now. So then, if on the first loop it does not find the search value it automatically defaults to the else statement? Then that is why it was returning false, if I'm understanding correctly... :?

-Thanks Steven!