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

Joannie Huang
Joannie Huang
6,195 Points

Why the result of search can't show immediately. It shows "after" I quit the prompt dialog..

Here is my code in the student_report.js

var message = '';
var student;
var search;

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

function getStudentReport(student){
    var report = '<h2>Student: ' + students[i].name + '</h2>';
    report += '<p>Track: ' + students[i].track + '</p>';
    report += '<p>Points: ' + students[i].points + '</p>';
    report += '<p>Achievements: ' + students[i].achievements + '</p>'; 
    return report;
}

while(true){
    search = prompt('Search Student\'s name like \'Jody\'. (or you can type \'quit\' to exit.)');
    if (search === null || search.toLowerCase() === 'quit'){
        break;
    }
    for(var i = 0; i< students.length; i++){
        if(search.toLowerCase() === students[i].name.toLowerCase()){
            message = getStudentReport(students[i]);
            print(message);
        }
    }
}

The messages will show "after" I dismiss the dialog, but not like the video shows, show it immediately when I clicked the "okay" button. Please help me to debug it! Thanks! :-)

4 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! The behavior of most browsers have changed since this video was made. It used to be that document.write while inside a loop would print to the document during the execution of the loop. This has been changed so that the document.write doesn't happen until after the execution of the loop completes.

You can find this under Important Update in the Teacher's Notes that accompany the video.

Hope this helps! :sparkles:

Joannie Huang
Joannie Huang
6,195 Points

Oh, Thank, you, Jennifer Nordell for quick explain. I learned it.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Thayer Y Sure! I started on February 1, 2016. You can read more about my journey learning to code on the Treehouse blog here. :sparkles:

But it doesn't look like Joanne is using document.write

Jennifer Nordell, Thank you for letting me know about "Treehouse blog" :)

Joannie, did you get this to work as expected? I had the same print function as you, which uses the innerHTML property, and I experienced the same results that you reported, namely that the student details only appear once, after you've gone through all your prompts, not after each name that you enter.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! While this was not addressed to me, I feel obligated to point out that you are correct, it is not using document.write. However, setting the innerHTML of an element still qualifies as "writing to the window" which will not occur until the completion of the while loop. I highly encourage you to read again the Teacher's Notes for the "Important Update". The instructor states unequivocally that your results will not match theirs in this regard.

Happy coding! :sparkles:

Joannie Huang
Joannie Huang
6,195 Points

Yes, Jennifer Nordell answered me and it's fixed.

Thanks, Jennifer!