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

Jamie Campbell
PLUS
Jamie Campbell
Courses Plus Student 7,603 Points

I can't find the mistake

I copied the code exactly from the video. When it loads, the prompt box doesn't close even when the typed answer is correct. Quit works, though.

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

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

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

}

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

3 Answers

Eric M
Eric M
11,546 Points

Hi Jamie,

while (true) will execute forever, unless there is a break statement.

Put a condition that can evaluate to true or false based on how you want to control the flow of the loop :)

Cheers,

Eric

Steven Parker
Steven Parker
231,248 Points

The prompt box should close as soon as you enter any answer. Whether it is "correct" or not shouldn't matter. That's how it performed for me, but then I had only this part of the code.

Looking at the complete code might reveal more. You can share it easily by using the "snapshot" function in the workspace (the camera icon), and then posting the link it gives you her.e

Gia Gabadadze
Gia Gabadadze
1,794 Points

i use var message empty string as a while loop "true or false" condition.

while(message === ''){
  search = prompt('type Students Name for Search or type "Quit" for exit from search');
  if(search === null || search.toLowerCase() === 'quit' ){
    break;
  }
for (var i = 0; i < students.length; i += 1){
  student = students[i];
  if(student.name === search){
    message = sTudentReport( student );
    print(message);
}
}
}