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 trialMichael Williams
Courses Plus Student 8,059 PointsI used a while loop to display search results with multiple entries by the same name. However, it's not working.
My while loop isn't working. When I enter a name that has more than one entry, the program times out.
AND before I put the while loop in, a search result would only display AFTER I quit the program, not right after I searched for it. Anywho, here's my code. Help?
var message = "";
var student;
var search;
function print(message) {
var outputDiv = document.getElementById("output");
outputDiv.innerHTML = message;
}
function getStudentReport(student) {
var report = "<h2>Student: " + student.name + "</h2>";
report += "<p>Track: " + student.track + "<p>";
report += "<p>Achievements: " + student.achievements + "<p>";
report += "<p>Points: " + student.points + "<p>";
return report;
}
while (true) {
search = prompt("Search for a student in our database. Type 'quit' to exit.");
if (search === null || search.toLowerCase() === "quit") {
break;
}
for (var i = 0; i < students.length; i += 1) {
student = students[i];
if ( student.name === search) {
while (student.name === search){
message = getStudentReport(student);
print(message);
}
} else {
message = "We couldn't find that student in our system."
print(message);
}
}
}
1 Answer
Ruben Ponce
Full Stack JavaScript Techdegree Student 12,035 PointsI noticed you're using two while loops. One of them is inside an if statement that test to see if student.name === search, using the same test condition. Check if that's the source of your error.
Michael Williams
Courses Plus Student 8,059 PointsIn the instruction video, the teacher wrote the first while loop, and I still couldn't get anything to display until after I quit the program. I just figured it was a Treehouse bug?
However, I put the second while loop in there to iterate through all the students that could be named, say, "Sarah" for example. So while there are Sarahs it would loop until it can't find any more Sarahs. So from that standpoint, I thought it would eventually end up false once it's exhausted everyone by that name.
But going back to the first while loop, how do I get it to display search results the instant I search like in the teacher's video? He had an infinite loop (that only stopped when you typed 'quit') but everything worked fine for him.
Charles Badger
18,189 PointsCharles Badger
18,189 PointsYou have an infinite loop in your code, that's why it's timing out and why nothing displayed until you quit the program.
In this loop, there is nothing that stops the condition from being true, which causes it to loop infinitely.