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!

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

Immanuel Jaeggi
Immanuel Jaeggi
5,164 Points

Code doesnt work completely

Any ideas why the prompt keeps repeating the request for a name and does not give the student info? Code comes up clean in the console. txs.

var message = ''; var student;

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: Type a name [Pete] into the search, or enter "quit" to exit the program'); 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); }

}

2 Answers

The same thing also is happening for me. Is it something with the browser...I am using Chrome? My solution was working except it wasn't printing out the search result until I exited with quit.

Øyvind Andreassen
Øyvind Andreassen
16,839 Points

Hi Immanuel,

You are checking if the students name is actually "search", and not if it's the variable search.

var message = ''; var student;

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: Type a name [Pete] into the search, or enter "quit" to exit the program'); 
  if (search === null || search.toLowerCase() === 'quit') break; 
} for (var i = 0; i < students.length; i += 1) { 
  student = students[i];

  if ( student.name === 'search') { // Look at this statement
    message = getStudentReport(student); 
    print(message); 
  }

}

------ EDIT -------

You are also not closing your loops and ifs properly. I should've caught that the first time around. Solution works here through workspaces. Though it doesn't print out the information in the background as in the video, and I can't see why.

var message = ''; var student;

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: Type a name [Pete] into the search, or enter "quit" to exit the program'); 
 if (search === null || search.toLowerCase() === 'quit') {break};

 for (var i = 0; i < students.length; i += 1) { 
  student = students[i];

    if ( student.name === search) { // Look at this statement
      message = getStudentReport(student); 
      print(message); 
    }


  } // Closing for loop
} // Closing while loop
Immanuel Jaeggi
Immanuel Jaeggi
5,164 Points

Hey thanks. Unfortunately, after making the change, the prompt continues to act up. :(