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

What's wrong with my loop?

I created this as the solution to the challenge to allow a user to search for specific students' records. I wanted to add in a line so that if the student name does not exist then it would return, "Student records for (student name) not found." However, it ends up printing that for every name that I search under the normal results that I wanted. It works fine if I take out the final else statement. What am I doing wrong?

Below is my code: (students is the name of the array containing the student records)

var studentList = '';
var student;
var search;

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

while (true) {
    search = prompt("What student would you like to search?");
    search = search.toLowerCase();
    if (search === null || search === "quit") {
        break;  
    } 
        for (var i = 0; i < students.length; i++) {
            student = students[i];
            if (search == student.name.toLowerCase()) {
                studentList += "<h2>Student: " + student.name + "</h2>";
                studentList += "<p>Track: " + student.track + "</p>";
                studentList += "<p>Achievements: " + student.achievements + "</p>";
                studentList += "<p>Points: " + student.points + "</p>";
            } else {
                studentList += "<p>Student records for " + search + " not found</p>";
            } 
        }
}

print(studentList);

4 Answers

var studentList = ''; var student; var search; var find = false;

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

while (true) { search = prompt("What student would you like to search?"); search = search.toLowerCase(); find = false; if (search === null || search === "quit") { break;
} for (var i = 0; i < students.length; i++) { student = students[i]; if (search == student.name.toLowerCase()) { studentList += "<h2>Student: " + student.name + "</h2>"; studentList += "<p>Track: " + student.track + "</p>"; studentList += "<p>Achievements: " + student.achievements + "</p>"; studentList += "<p>Points: " + student.points + "</p>"; find = true; } } if(!find) { studentList += "<p>Student records for " + search + " not found</p>"; } }

print(studentList);

Your loop displays this because every time you look for a student it iterates through the loop and even if the student is in the table assume it is in the last rank when it compares the first and search the program goes into The else

I made a solution but i don't know how to upload the picture in the comment

Thanks for the simple answer! Got it fixed and everything is working perfectly now :)

You're welcome