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

Randy Singh
seal-mask
.a{fill-rule:evenodd;}techdegree
Randy Singh
Full Stack JavaScript Techdegree Student 1,462 Points

RealTime Response & User Entry

My code doesnt print out the students records until after I enter "quit" or hit Cancel. I researched and found the setTimeOut method but dont know where in my code to place it or parameter it should take?

Also, I notice that typing my entry as all lowercase (jody) will not return Jody's records as is with the other student records. Only if i set each students name to all lowercase letters and use: student.name === search.toLowerCase() will it work. How come it works in Dave's video but not mines?

Here is my students.js file

var students = [ 
  { 
    name: 'dave',
    track: 'Front End Development',
    achievements: 158,
    points: 14730
  },
  {
    name: 'Jody',
    track: 'iOS Development with Swift',
    achievements: 175,
    points: 16375
  },
  {
    name: 'Jordan',
    track: 'PHP Development',
    achievements: 55,
    points: 2025
  },
  {
    name: 'John',
    track: 'Learn WordPress',
    achievements: 40,
    points: 1950
  },
  {
    name: 'Trish',
    track: 'Rails Development',
    achievements: 5,
    points: 350
  }
];

And here is my student_report.js file

//hold a string of html that build up in the loop and print our to the document
var message = ' ';

//use this variable in the loop to hold th students object each time the loop runs
var student;

//creates a variable to search on
var search;


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


//print out a message showing the student data
//this function has one parameter: student that will return the students data
function getStudentRecord ( student ) {
  //create a variable inside the function named report to print out the student data
  var report = '<p>My name is ' + '<b>' + student.name + '</b>' + '</p>';
  //add on to the report variable with other student data
  report += '<p> I am taking the ' + student.track + ' track </p>';
  report += '<p> I have ' + student.achievements + ' achievements </p>';
  report += '<p> And have accumulated ' + student.points + ' points </p>';
  //return the final string contained in the report variable
  return report;
}



//while loop will repeatedly prompt me to provide input in a dialog box until I click Cancel or type Quit
while ( true ) {

  //prompt user
  search = prompt('Search student records: type a name [Jody] (or type "quit" to exit the       program');

  //break out of program if Cancel is clicked or user types "quit"
  if (search === null || search.toLowerCase() === 'quit' ) {
    break;
  }

  for ( var i = 0; i < students.length; i += 1) {
  //compare actual student name with name user typed in dialog box
  student = students[i];
  if ( student.name === search  ) {
    //call the getStudentRecord function which has the strings of all the student data and store that in the message variable which will print out to the web page
    message = getStudentRecord ( student );
    print(message);
  } 
}

}
Anastasia Stojakova
Anastasia Stojakova
8,974 Points

I have the same problem with prompt.

1 Answer

Ginger Cullen
Ginger Cullen
4,301 Points

Almost all of the questions in this section are about not seeing the results of the search until you type 'quit' or hit cancel. Most answers suggest using setTimeout, but I think that is beyond the scope of this course. I think that Manav Misra had the best suggestion: use Firefox. That's the browser Dave was using. It does work for me in Firefox (as of version 48.0.2 on a Mac). It doesn't work in Chrome or Safari.

One other thing, Dave typed 'Jody' with a capital J. He didn't type it in all lower case. That's why the Jody search works for him.