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

Derick Brown
Derick Brown
7,361 Points

What are the differences between Dave's code and my code?

I'm a little lost with this challenge as I'm not sure what the differences are between the code Dave wrote and the code I produced.

Either way it goes, Dave and I produced the same results without any syntax errors.

If you run my code, you're able to search for a student and generate their records. If you quit then you quit the search. In other words, I pretty much passed this challenge lol.

My question to the JavaScript gurus out there is what's the difference between both of our codes?

We wrote 2 completely different codes and produced the same results. I know there's more than one way to skin a cat but was there any benefits to doing it Dave's way? Maybe it's more dynamic and easier to update? Was my way more open for errors? Maybe my method wasn't the correct "industry standard" way of coding?

Please let me know!

Dave's Code:

var message = '';
var student;
var search;

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

function getStudentReport(student) {
  var report = '<h2>Student: ' + student.name + '</h2>';
  report += '<p>Track: ' + student.track + '</p>';
  report += '<p>Points: ' + student.points + '</p>';
  report += '<p>Achievements: ' + student.achievements + '</p>';
  return report;
}

while (true) {
  search = prompt('Search student records: type a name [Jody] (or type "quit" to end)');
  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);
    }
  }
}

My Code:

var message = '';
var student;
var studentSearch = prompt('Search student records: type a name [Jody] (or type "quit" to end)');

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

for (var i = 0; i < students.length; i += 1) {
  student = students[i];
  if (studentSearch.toUpperCase() === student.name.toUpperCase()) {
    message += '<h2>Student: ' + student.name + '</h2>';
    message += '<p>Track: ' + student.track  + '</p>';
    message += '<p>Points: ' + student.points + '</p>';
    message += '<p>Achievements: ' + student.achievements + '</p>';
  } else if (studentSearch.toUpperCase() === 'QUIT') {
    break;
  }
};

print(message);

Update So far the only difference I found is my prompt loops breaks once you match a name in the object. Because of how updated browsers are today, you won't see the results with Dave's code until you quit. Which this is something that was pointed out in the earlier lessons.