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

martin wong
martin wong
4,799 Points

My code wont work, and wont print at all?

var message;
var search;
var student;

var students = [
  {Name: "John", Track: "Front-end development", Points: 4457, Achievements: 23},
  {Name: "Mika", Track: "iOS", Points: 2421, Achievements: 12},
  {Name: "Keenan", Track: "Ruby", Points: 8788, Achievements: 47},
  {Name: "Louise", Track: "JavaScript", Points: 5390, Achievements: 28},
  {Name: "Elise", Track: "Java", Points: 7639, Achievements: 35}
];

while (true) {
  search = prompt("Search Student records: Type a name [Kevin] or type [quit] to end or   [list] to list all student records");
  if (search === null || search.toLowerCase() === "quit") {
  break; 
} 
for (var i = 0; i <students.length; i += 1) {
  student = students [i];  
  if (student.Name === search.toLowerCase()) {
    message = getStudentReport(student);
    print (message);
    }
  }
}


function getStudentReport (student) {
 var html = " <h2> Student name: " + student.Name + "</h2>";
  html += " <p> Track: " + student.Track + "</p>";
  html += " <p> Points: " + student.Points + "</p>";
  html += " <p> Achievements: " + student.Achievements + "</p> </br>";
  return html;
}

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

I have looked on jslint and the console and cannot find any errors. I have also compared my code with Dave's code and cannot find any differences apart from the names that i name the variables etc. When i type in a name e.g. "elise", the prompt continues to pop up

What am i doing wrong?

Thanks

2 Answers

Thomas Nilsen
Thomas Nilsen
14,957 Points
if (student.Name === search.toLowerCase()) {
    message = getStudentReport(student);
    print (message);
}

you never add lowercase to student.Name. So if you search for "elise", the if check will be Elise === elise, which is false, and the while-loop goes on.

martin wong
martin wong
4,799 Points

Thanks! i have changed the code to

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

My code is still not working though, i have quite literally copied Daves now yet it will not work. Im very confused right now

var message;
var search;
var student;

var students = [
  {Name: "John", Track: "Front-end development", Points: 4457, Achievements: 23},
  {Name: "Mika", Track: "iOS", Points: 2421, Achievements: 12},
  {Name: "Keenan", Track: "Ruby", Points: 8788, Achievements: 47},
  {Name: "Louise", Track: "JavaScript", Points: 5390, Achievements: 28},
  {Name: "Elise", Track: "Java", Points: 7639, Achievements: 35}
];

while (true) {
  search = prompt("Search Student records: Type a name [Kevin] or type [quit] to end or   [list] to list all student records");
  if (search === null || search.toLowerCase() === "quit") {
  break; 
} 
for (var i = 0; i <students.length; i += 1) {
  student = students [i];  
  if (student.Name === search) {
    message = getStudentReport(student);
    print (message);
    }
  }
}


function getStudentReport (student) {
 var html = " <h2> Student name: " + student.Name + "</h2>";
  html += " <p> Track: " + student.Track + "</p>";
  html += " <p> Points: " + student.Points + "</p>";
  html += " <p> Achievements: " + student.Achievements + "</p> </br>";
  return html;
}

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

Pretty much the same to Dave's?

Edit: I noticed in my first code, if i type in the name in perfect letters e.g. "Mika" not "mika" then my code will work But i have to type "quit" to see my student info. In Dave's video, the student info appears without him having to type in "quit", is this normal or am i doing something wrong?

Neil McPartlin
Neil McPartlin
14,662 Points

Hi Martin. You are a little unlucky and the experience you are having is to be expected. Dave has updated his teacher's notes to explain the following...

https://teamtreehouse.com/library/the-student-record-search-challenge-solution

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.

Erik Robles
Erik Robles
Courses Plus Student 10,635 Points

I'd also like to note that I had the same problem and used the Workspace link in Firefox whick worked much better. I believe Martin was experiencing a browser issue more than anything else. Also, You have to type in the name with upper case or the program just won't run right either. I would love to know what code I could implement to make it work with either upper or lowercase in the prompt.