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

Connie Lu
seal-mask
.a{fill-rule:evenodd;}techdegree
Connie Lu
Front End Web Development Techdegree Student 4,379 Points

Why doesn't my code work?

I see the solution the instructor provided, and mine is similar but not quite the same. I'm wondering why my code doesn't work. Isn't it correct that the while loop should continue to run while the search input isn't 'quit'? Also, since JS reads top to bottom, shouldn't the print(message) be called and printed to the page before the next iteration of the while loop?

var message = '';
var student;

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

var search;

while(search !== 'quit') {
  search = prompt('Search for a student');
  for (var i = 0; i < students.length; i += 1) {
    student = students[i];
    if(search === student.name) {
      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);
    }
  }
}

1 Answer

Antony .
Antony .
2,824 Points

This is because the behavior of browsers have changed how they handle loops just across the board.

Under the video there should be a section called "Questions". There should be some comprehensive and detailed answers if you want to know more.