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 trialnagamani palamuthi
6,160 Pointssearch record code challenge displays only the last name's record typed before 'quit'.
4 Answers
Chung-Jun Wang
3,718 PointsI am not sure why in the video it works, but still, the reason is that prompt method in the while loop stop browser before it finishes rendering new content on the page, if you look into the development tool, you will find HTML DOM actually has been changed. So the trick i do is using setTimeout pause the second prompt a little bit, so browser has time to finish the job.
var message,student,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>Points: ' + student.points + '</p>';
report += '<p>Achievements: ' + student.achievements + '</p>';
return report;
}
promptSearch(); //excute function first time;
function promptSearch(){
var flag = true;
while(flag){
search = prompt("type a name: (type 'quit' to exit)");
if(search.toLowerCase() === 'quit' || search === null ){
break;
}
for (var i = 0; i < students.length; i += 1) {
student = students[i];
if (student.name === search){
message = getStudentReport(student);
print(message);
flag = false; //stop the while loop first
setTimeout(promptSearch,100); //wait 0.1 sec then prompt again
}
}
}
}
Steven Parker
231,275 PointsI warned you about this in my answer to your previous question.
Did you see that answer? I'll rephrase it here to fit the current status of the code:
Your for loop replaces the value in message for each match, so only the name of the last student matched ("Trish") will be in the variable. Then the print function replaces the value of the output element, so even though you call it inside the loop, after the loop is done you'll only see the last student.
To alter this behavior, you'll need to change at least one of these so that it adds to the text instead of replacing it completely.
Owa Aquino
19,277 PointsHi Steven!
I think his issue is that when you entered 'quit' its the only time the Student Record is displayed in the browser.
I have the same issue :(
nagamani palamuthi
6,160 Pointsoh thats a good idea. Thanks. but wonder why in the lesson it works...
Jefferson Snow
1,357 PointsI had this issue on a previous loop challenge and on this challenge as well. I will enter in one of the student's name and the prompt reappears. Then I hit quit and the page prints out the student's info that I put during the last prompt.