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

Parker Brown
Parker Brown
24,308 Points

Code won't display

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 students records: type a name (or "quit" to end) '); 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);
} }
}

Here's my code. The exact same as Dave's in the solution's video, and yet it still won't display anything other than the prompt. I've put the code thru CodePen, JSBin and Sublime Editor trying to use a different browser, from Firefox to Chrome. I've also opened several instances of WorkSpaces. To no avail. I've been having display issues thru out several points in this track. Anyone else in the same boat, or have I overlooked some faulty code? Any feedback would be greatly appreciated. Thanks!

3 Answers

Dom Talbot
Dom Talbot
7,686 Points

Hey Parker

It appears your for loop is missing a few parts.

This should help:

while (true) {
    search = prompt('Search students records: type a name (or "quit" to end) ');
    if (search === null || search.toLowerCase() === 'quit') {
        break;
    }
    // What you had initially was:
    // for (var i = 0; i student = students[i];
    for (var i = 0; i < students.length; i +=1 ) {
        student = students[i];
        if (student.name === search) {
            message = getStudentReport(student);
            print(message);
        }
    }
}
Parker Brown
Parker Brown
24,308 Points
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 students records: type a name (or "quit" to end) ');
  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);    
  }
 }  
}
Parker Brown
Parker Brown
24,308 Points

Sorry I didn't post my code correctly using the proper Markdown for JS. Here is what I've been attempting to run. Thanks for any feedback.

Cheers

Dom Talbot
Dom Talbot
7,686 Points

Your code works fine for me.

Are you entering the names, as they appear in the students list - i.e with the capitalised letter? So John instead of john? The search names need to be written exactly as they are in the list.

Parker Brown
Parker Brown
24,308 Points

BOOM! That did it! Thanks Dom for your help. It works fine now. Strange I hadn't accidentally typed them that way before. Oh well. You code and learn. Appreciate it.