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

While loop problem - Using function to search for student. Appreciate any thoughts/solution.

Hi,

I wanted to write functions to either pull all student-records or to search for any student-record. I've been staring blindly at code for quite some time. I can find the record of a student by typing the name in the prompt and then quit, but I can't seem to check if student.name exists while looping. If the student exists then break off the loop. If not - keep looping but probably with a message saying no record found. Any tips/thoughts are greatly appreciated

var studentRecords = [{
  name: 'Andrew',
  track: 'Android',
  achievements: 0,
  points: 1000,
}, {
  name: 'Ian',
  track: 'iOS',
  achievements: 3,
  points: 30,
}, {
  name: 'Sarah',
  track: 'SQL',
  achievements: 58,
  points: 580,
}, {
  name: 'Joshua',
  track: 'JavaScript',
  achievements: 100,
  points: 1000,
}, {
  name: 'Peter',
  track: 'Python',
  achievements: 5698,
  points: 56980,
}, ];

var studentList = '';

function getAllStudentRecords() {

  for (var i = 0; i < studentRecords.length; i++) {
    var student = studentRecords[i];
    studentList += '<ul>';

    for (var prop in student) { // Generate list through loop
      studentList += '<li>' + prop.charAt(0).toUpperCase() + prop.substr(1).toLowerCase() + ': ' + student[prop] + '</li>';
    }
    studentList += '</ul>';
  }
  return studentList;
}

function getStudentByName(studentname) {

  for (var i = 0; i < studentRecords.length; i++) {
    var student = studentRecords[i];
    studentList += '<ul>';

    if (student.name === studentname) {
      for (var prop in student) { // Generate list through loop
        studentList += '<li>' + prop.charAt(0).toUpperCase() + prop.substr(1).toLowerCase() + ': ' + student[prop] + '</li>';
      }
    }
    studentList += '</ul>';
  }
  return studentList;
}

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


while (true) {
  var command = prompt("Type a name to show student record\nType GETALL to list all student records\nType Q to quit.");
  print(getStudentByName(command));
  if (command === null || command.toUpperCase() === 'Q') {
    break;
  } else if (command.toUpperCase() === 'GETALL') {
    print(getAllStudentRecords());
    break;
}

2 Answers

Here's what Treehouse says about the issue you're having:

"Important Update: Since this video was shot, the behavior of most browsers has changed, so you won't see the same thing as I demonstrate in the video. In the video, you'll see that my script is able to print out to the browser using document.write( ) while inside a loop.

Most browsers no longer do that: they wait until the loop finishes and then they print to the window. So, you'll see a blank page until you type quit in the prompt window — then you'll see all the output printed to the screen."

Hope that helps!

Dammit... didn't read the teachers notes for this one. Thanks for pointing it out.

The real problem is that I can't seem to figure out how to check if student.name exists. If the student exists then break off the loop. If not - keep looping but probably with a message saying no record found.

Steven Parker
Steven Parker
243,656 Points

:point_right: You might not want to break out of the loop.

There could be more than one student with the same name. But you could initialize a "found" variable to false before you begin, and set it to true when you find a match. Then after the loop, you can check this variable to see if there was (one or more) record found.

Any necessary communication while inside the loop can be done using alert().