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

Petter Östergren
Petter Östergren
6,920 Points

Values get displayed when prompt is closed but not before

I have a data list

var students = [
  {
    name: 'Petter',
    track: 'Mobile Design',
    achievements: '49',
    points: '3400',     
  },
  {
    name: 'Charlie',
    track: 'Front-End Web Developer',
    achievements: '29',
    points: '890',     
  },
  {
    name: 'Max',
    track: 'Android Dev',
    achievements: '89',
    points: '12340',     
  },
  {
    name: 'Linus',
    track: 'Design',
    achievements: 102,
    points: '14200',     
  },
  {
    name: 'Tuvis',
    track: 'Front-End Web Developer',
    achievements: '80',
    points: '10982',     
  },
]

And am supposed to build a student record that can search but my items are only displayed after I close my prompt why do I get this behavior?

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

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

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

while(true) {
  search = prompt('Search student records: typa a name [Tuvis] (or type "quit" to exit)')
  if (search === null || search.toLowerCase() === 'Quit') {
    break;
  }
  for (var i = 0; i < students.length; i += 1) {
    student = students[i];
    if (student.name === search) {
      message = getStudentReport(student);
      print(message);
    } 
  }                    

}



print(message);
Matthew Long
Matthew Long
28,407 Points

The only issue I see off the bat is it will be impossible to type quit to exit. You need to use search.toLowerCase() === 'quit'. If I remember correctly, the next videos will help you getting more results to show than just one at a time.

Petter Östergren
Petter Östergren
6,920 Points

Thanks, Matthew, missed that just changed it for testing. Nicely spotted.

1 Answer

Steven Parker
Steven Parker
231,275 Points

It's normal for modern browsers to not render the page content until after the JavaScript program has finished running. This is a behavior change from the concurrent operation that was common back when the video was produced, and is explained in the Teacher's Notes section at the bottom of the video page.

Petter Östergren
Petter Östergren
6,920 Points

Oh Thank you, Steven clearly needs to pay more attention to teachers notes. Missed that part, will have a read about it. Once again thanks!