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

Lucas Guimarães
seal-mask
.a{fill-rule:evenodd;}techdegree
Lucas Guimarães
Front End Web Development Techdegree Student 3,900 Points

Question about my print code on the page

I am in trouble with my print(message). When I type the name`s student it does not appear on the page. Only when I am out of the loop, all the correct names I typed show up in one goal. I am struggling to understand the logic for that. Because it is inside of a conditional and if it is true, the print(message), should come up on the page.

PS: I forgot how to post my code here :/ Sorry

var message= "";
var userMessage= "";
var studentName= "";

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

while (userMessage !== "quit") {

  userMessage = prompt("Please, enter the name of the student. Or type 'quit' to exit.");

  for (i=0; i < students.length; i+=1) {  

studentName = students[i].name;

  if (userMessage === studentName) {
message += "<h2><b> Student: " + students[i].name + "</b></h2>";
message += "Track: " + students[i].track + "<br>";  
message += "Achievements: " + students[i].achievements + "<br>";  
message += "Points: " + students[i].points;
print(message) 
  }
}  
}

1 Answer

Steven Parker
Steven Parker
231,275 Points

That's normal browser behavior. If you look down at the "Teacher's Notes" at the bottom of the video page linked to this question, you'll see this:

Important Update

Since this video was shot, the behavior of most browsers has changed, so you won't see the same thing as I demonstrate in the video. In the video, you'll see that my script is able to print out to the browser using document.write( ) while inside a loop.

Most browsers no longer do that: they wait until the loop finishes and then they print to the window. So, you'll see a blank page until you type quit in the prompt window — then you'll see all the output printed to the screen.

You'll soon learn other techniques that provide immediate interaction on the page in modern browsers.