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

Kyle Zunino
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree seal-36
Kyle Zunino
Full Stack JavaScript Techdegree Graduate 23,214 Points

Browser not displaying information like Dave's example, even after moving print(message) outside of the while loop.

I did the first two parts of the challenge with my own code, however, I copied Dave's code after I couldn't get the information to display on the browser. I read the teacher's notes late, and I still cant seem to get the browser to display the information correctly. If I move the print(message) outside of the while loop, only Trish's information prints to the browser after I exit the loopโ€”regardless if I type a name listed in the object or not. I'm kinda out of ideas as to what to do.

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("Please type a name to retrieve student info. To exit type 'quit.'");
  if (search === null || search.toLowerCase() === 'quit'){
   break;

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


//message needs to be outside of while loop
 message = getStudentReport (student);
 print(message); 

2 Answers

For your search are you typing the names in title case? If not you could change the comparison of student.name === search to student.name.toLowerCase() === search.toLowerCase().

Kyle Zunino
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Kyle Zunino
Full Stack JavaScript Techdegree Graduate 23,214 Points

Thanks for the reply. I've been typing them in lowercase to the prompt, but I added the .toLowerCase method to play around with the functionality. I added your suggestion, but it doesn't seem to change the issue if I make that change.

EDIT: Yes that worked. Thanks! That was a grueling 2 hours =)

You need to move the code back to the if statement as well

if ( student.name.toLowerCase() === search.toLowerCase()) {
    message = getStudentReport (student);
    print(message); 
}

Hey Kyle, I think your main problem here is that your getStudentReport() call had to be inside your for loop, right now your for loop if statement is not doing anything unfortunately.

for (var i = 0; i < students.length; i += 1) {
  student = students[i];
  if ( student.name === search) {
     message = getStudentReport(student);

  }
}
}


//message needs to be outside of while loop

 print(message); 

When searching for a valid student name, the prompt pops up again, if you press cancel on the prompt the name and info of the student should be there. I think getting the prompt to not appear was some of the added functionality they talked about in the video, but atleast your main function is working.

Hope this helps, Dylan