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

Student Record Solution

Resolved: If there are two students with the same name, both name records appear. Resolved: If the record does not exist message on HTML that no student found. Bonus: When quit is entered, display Good bye

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

 var response = '';
do{
  var message = '';

  response = prompt('Search student records: type a name Jody(or type quit to end)');

 var found = false;   
for (var i = 0; i <students.length; i += 1)
{ 
  if (students[i].name === response)
  {
   message += '<h2>Student: ' + students[i].name + '</h2>'; 
   message +='<p>Track:  ' +  students[i].track + '</p>';
   message +='<p>Points:  ' +  students[i].points + '</p>';
   message +='<p>Achievements:  ' +  students[i].achievements + '</p>';

  print(message);
  found = true;

  }  
} 
  if (!found){
    message +='<h2>No student found by that name.</h2>'; 
  }
  print(message);

} while (response.toLowerCase() !== 'quit' || response === null)

 print('Good bye');

1 Answer

Hi Denise Diebold. First up, just letting you know that I wrapped your code in code blocks to make it easier to read.

Second, I can't be sure without making a copy and testing it, but I believe if you use a do..while loop it will always run the code at least once, so even if the user wrote 'quit' or didn't enter any value on the first iteration of the loop, it would still try and execute the other code.

In that case, it would either print 'No student found by that name.' or it would throw an error about a null object not having the toLowerCase() method...

Maybe give that a try and see what happens?