JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects The Student Record Search Challenge Solution

cristiancorrales
cristiancorrales
Front End Web Development Techdegree Student 7,791 Points

My Extra Credit Solution

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


do{
  reply = prompt("Please enter a name");
  if(reply === null || reply === ''){
  break;
  }
  for(var i = 0; i < students.length; i++){
    if(students[i].name === reply){
      counter+= 1;
      if(counter === 2){
        message += "<br><h3>Name: " + students[i].name + "</h3>";
      }
      else {
      message = "<h3>Name: " + students[i].name + "</h3>";
      }
      message += "<h3>Track: " + students[i].track + "</h3>";
      message += "<h3>Achievements: " + students[i].achievements + "</h3>";
      message += "<h3>Points: " + students[i].points + "</h3>";
      print(message);
    }
  }

  counter = 0;
      }while(reply !== 'quit')
Michael Murray
Michael Murray
6,037 Points

I see that you are checking for two students with identical first names. What if there are more than 2? Also, Dave was asking what if a student name was entered that was not in the list of students - he wanted a message sent to the screen alerting the user of this.

I liked the way you used the counter. I added it to my code as you can see below.

var message = ''; var search; var student; // Holds one object at a time. var studentLower; // Holds lowercase student name. var found; // Will use to report that name does not exist. var counter = 0; // Will use to tally duplicate student names.

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

while ( true ) { // CREATED AN ENDLESS LOOP HERE found = false; // Set found flag here for the "Sorry message" below.

search = prompt("Type the student's first name to find info and 'quit' to exit.");

if ( search === null || search.toLowerCase() === "quit" ) { break; // Null would happen if the user clicked on the prompt's cancel button.

} else { // Let's find the object in the array with the correct name.

for (var i = 0; i < students.length; i += 1) {
  student = students[i];    //  Looking into each of the objects.
  studentLower = student.name;  //  Loads the .name field into our variable
                                //    then we actually confert it to lowercase next.
  if ( search.toLowerCase() === studentLower.toLowerCase() ) {     // If found then
                                                                   // print out data.
    found = true;         //   Flag changed here to not run the "Sorry message" below.
    counter += 1;         //   Gets incramented each time a match is found.
    if ( counter > 1 ) {
      message += "<h2>Student: " + student.name + "</h2>";  //  If this is a duplicate.
    } else {
      message = "<h2>Student: " + student.name + "</h2>";  //  First time name found.
    }

    message += "<p>Track: " + student.track + "</p>";
    message += "<p>Points: " + student.points + "</p>";
    message += "<p>Achievements: " + student.achievements + "</p>";  
    print( message );

  }      //  End of If
}      //  End of for loop
counter = 0;                    //  Resetting
message = '';                   //  Resetting

} // end of else if ( found === false ) { message += "<h2>Sorry, " + search + " is not in this database. Are you sure that you capitalized the first and only the first letter of the name?</h2>"; print( message ); }
} // End of while loop