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

Arnon Saengsee
Arnon Saengsee
3,638 Points

Why i have to type "exit" to finish the loops before seeing student information

if i code like this can anyone tell me what's the reason that my student information will show after typing "exit" to finish the loop. i want to see it during a loop. Thanks guys

var message = '';
var studentSearch;

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

while (true) {
 var search = prompt("What is your student's name are you looking for type exit to end searching"); 
  if(search === 'exit') {
    break;
  }
  for (var i = 0; i < students.length; i += 1) {
    studentSearch = students[i]
    if(studentSearch.name === search) {
        message = '<h2>Student: ' + studentSearch.name + '</h2>';
        message += '<h2>Track: ' + studentSearch.track + '</h2>';
        message += '<h2>Points: ' + studentSearch.points + '</h2>';
        message += '<h2>Achievements: ' + studentSearch.achievements + '</h2>'; 
        print(message);
    }
  }
}

1 Answer

Steven Parker
Steven Parker
230,995 Points

Your browser doesn't render the page until the program has finished running. The only reliable way to display information while the program runs is with "alert", "prompt" or "confirm" modal (pop-up) boxes.

But as you continue with the courses, you'll soon learn other techniques to make your pages fully (and immediately) interactive.