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

An Phung
seal-mask
.a{fill-rule:evenodd;}techdegree
An Phung
iOS Development Techdegree Student 3,913 Points

Student report didn't show up until type quit???

I don't know why my student report doesn't show up right after I typed a student name. But it will show up if I typed quit and break the loop?

var message = '';
var student;
var ask;

function getReport(student){
    message += '<h2>Student: '+ student.Name+'</h2>';
    message += '<p>Track: '+ student.Track+'</p>';
    message += '<p>Grade: '+ student.Grade+'</p>';
    document.write(message);
}

while (true) {
    ask = prompt("please type name, type quit to quit");

    if (ask.toLowerCase()==='quit'||ask===null) {
        break;
    }

    for (var i=0; i < students.length; i+=1) {
        student= students[i];
        if (ask === student.Name.toLowerCase()) {
            getReport(student);

        }
    }
}

5 Answers

Hey An,

I have this curious feeling that you are using Safari to test your code. Am I correct in saying that?

An Phung
seal-mask
.a{fill-rule:evenodd;}techdegree
An Phung
iOS Development Techdegree Student 3,913 Points

Oh Yes! I am using Safari. Is it the reason for what's happing with my code? And do you know how I can fix it or should I just have to change my browser? Thank you!!

It's one of those peculiarities of Safari. Safari doesn't output to the screen until the loop is done, and it's the only browser I've seen do that thus far.

Try taking your document.write() method and converting it into a method that stores the data in an element. To get you started, let's say you have a div element with an id of "output": <div id="output"></div>> so in order to change that div's inside HTML, you can call upon the "innerHTML" method after selecting that div using the "getElementById" method:

var message = '';
var student;
var ask;

function getReport(student){
    message += '<h2>Student: '+ student.Name+'</h2>';
    message += '<p>Track: '+ student.Track+'</p>';
    message += '<p>Grade: '+ student.Grade+'</p>';
    document.getElementById('output').innerHTML += message;
}

while (true) {
    ask = prompt("please type name, type quit to quit");

    if (ask.toLowerCase()==='quit'||ask===null) {
        break;
    }

    for (var i=0; i < students.length; i+=1) {
        student= students[i];
        if (ask === student.Name.toLowerCase()) {
            getReport(student);

        }
    }
}

I don't know if that will allow output to the leak out to the screen. I can't use Safari since it's a Mac only product now :\

An Phung
seal-mask
.a{fill-rule:evenodd;}techdegree
An Phung
iOS Development Techdegree Student 3,913 Points

It still doesn't allow the output to show on the screen. But thank you so much, I thought I did something wrong but turned out Safari is the cause.

It's still a good browser to use, but it does have those peculiarities like that. Happy Coding, An! =]

niksalleh
niksalleh
2,827 Points

I'm having the same problem, the output happens only after quitting the while loop. I'm using Chrome Version 54.0.2840.59 m. Is there a workaround?

This is my code:

var message = ''; var student;

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

var FindName = ' '; while (true) { FindName = prompt('Search student records: type a name "Jody" (or type "quit" to end)'); FindName = FindName.toUpperCase(); if (FindName === "QUIT") { document.write("Search ended"); break; } for (var i = 0; i < students.length; i += 1) { student = students[i]; message = ' '; if (FindName === ((student.name).toUpperCase())) { message += '<h2>Student: ' + student.name + '</h2>'; message += '<p>Track: ' + student.track + '</p>'; message += '<p>Points: ' + student.points + '</p>'; message += '<p>Achievements: ' + student.achievements + '</p>'; print(message); } } }

Kyle Cameron
Kyle Cameron
20,683 Points

I had this issue as well with the student record not showing until 'quit' was entered. I was using chrome to preview the code I wrote in webstorm, not workspaces, and nothing was displaying. I ended up using the Microsoft Edge browser to preview my work and it displayed perfectly. Based on my review of this thread and other similar threads it appears that both Chrome and Safari wont display dynamically inserted data, via a loop, until the loop is cancelled. Whether this is version specific or not I don't know. I hope this helps those who read.