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

Ben Dooby
Ben Dooby
6,176 Points

Student record search challenge. Output does not show until I enter quit. Does work in console though,

Whenever I type a name in the prompt nothing happens until I press the cancel button or type quit. However it seems to work normally in the javascript console. Not sure if it is just my browser or if it is an issue in my code.

This is my Javascript Search Code:

var message = ""; var answer;

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

function searchStudents( studentName) { var report = "<h2>Name: " + students[i].name + "</h2>"; report += "<p>Track: " + students[i].track + "</p>"; report += "<p>Achievements: " + students[i].achievements + "</p>"; report += "<p>Points: " + students[i].points + "</p>"; return report; }

while (true) { answer = prompt("Search for a student by name, type quit to end"); if (answer === null || answer.toLowerCase() === "quit") { break } for (var i = 0; i < students.length; i++) { if (students[i].name.toLowerCase() === answer.toLowerCase()) { document.write(searchStudents(answer)); console.log(searchStudents(answer)); }
} }

This is my javascript object:

var students = [ { name: "Victor", track: "Front End Web Development", achievements: 20, points: 15 }, { name: "Ben", track: "Full Stack Javascript", achievements: 22, points: 17 }, { name: "Dooby", track: "Python Django", achievements: 23, points: 1 }, { name: "Morland", track: "ASP.NET", achievements: 40, points: 29 }, { name: "Dan", track: "Web Design", achievements: 0, points: 0 } ];

This comment is from another person I just saw his comment why the browser keep reloading the prompt method until you type "quit" so here is the solution =>

This comment is from : Chung-Jun Wang , May be he is a student or moderator don't know.

I find a lot of people has the same problem, i have answered my solution in the previously question, but still, post here again, wish this help, you can find my explain in the previously question.

It is really not a common way in the real world use prompt method in the while loop, because prompt stop browser processing code and affect code work flow. When it comes to ask users input data, coders usually custom their own dialog, but this require other skill.

So teacher use prompt here just because this is the easiest way ask user input something.

click on the link below and """""""OPEN help.js"""""""" to see the codes how to solve that problem.

==> https://w.trhou.se/6wxsb2onqz

3 Answers

Steven Parker
Steven Parker
231,261 Points

:point_right: Your browser is waiting for the page to finish loading.

Inline JavaScript code is is considered part of page loading, and until the loop exits, the loading is considered incomplete. When you type "quit", the loop ends, and the browser then displays everything that has been written to the document.

Ben Dooby
Ben Dooby
6,176 Points

So how do I fix this? Also what is inline js code? As when I use the downloaded code from the video the same issue occurs.

Nguyen Hao
Nguyen Hao
3,579 Points

I have same question. How can I fix it? How can I make it run like teacher's example?

This comment is from another person I just saw his comment why the browser keep reloading the prompt method until you type "quit" so here is the solution =>

This comment is from : Chung-Jun Wang , May be he is a student or moderator don't know.

I find a lot of people has the same problem, i have answered my solution in the previously question, but still, post here again, wish this help, you can find my explain in the previously question.

It is really not a common way in the real world use prompt method in the while loop, because prompt stop browser processing code and affect code work flow. When it comes to ask users input data, coders usually custom their own dialog, but this require other skill.

So teacher use prompt here just because this is the easiest way ask user input something.

click on the link below and """""""OPEN help.js"""""""" to see the codes how to solve that problem.

==> https://w.trhou.se/6wxsb2onqz

Nguyen Hao
Nguyen Hao
3,579 Points

@Gurpreet Kuar - Clear! Thanks you!

Steven is right. The browser physically can't finish loading because the javascript was triggered before the end of the page was finished loading. I can only assume that in later lessons in this course plan they talk more about that and show how to trigger more functional code after the load.

This comment is from another person I just saw his comment why the browser keep reloading the prompt method until you type "quit" so here is the solution =>

This comment is from : Chung-Jun Wang , May be he is a student or moderator don't know.

I find a lot of people has the same problem, i have answered my solution in the previously question, but still, post here again, wish this help, you can find my explain in the previously question.

It is really not a common way in the real world use prompt method in the while loop, because prompt stop browser processing code and affect code work flow. When it comes to ask users input data, coders usually custom their own dialog, but this require other skill.

So teacher use prompt here just because this is the easiest way ask user input something.

click on the link below and """""""OPEN help.js"""""""" to see the codes how to solve that problem.

==> https://w.trhou.se/6wxsb2onqz