Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

saul bard
saul bard
7,581 Points

Feedback on student record challenge

Hello everyone,

I have attached the snapshot of my code for this challenge. It works which is awesome but would like to get some feedback on my code and whether or not there is a more efficient way to do it.

https://w.trhou.se/ajgffpqosi

Thanks, Saul

1 Answer

Elad Ohana
Elad Ohana
24,453 Points

Hi Saul,

I tried to keep your style mostly intact and came up with a slightly different way of going through this problem. Instead of creating a new array with the names, I simply set up the function to search through the original list itself. I then returned the value of the student and passed it onto a variable, of which I then extracted the properties (name, track, points...)

//// Declaring my variables - using let instead of var, but either one works
let html = ''; // took out space in between the quotes
let searchName;

// Function to search through list of students
// Parameter of name to be searched for
// Parameter of list for the array in which to search
// returns the item found in the array or null if none is found
function searchStudent(name, list){
  for (item of list){
    if (item.name.toLowerCase() === name.toLowerCase()){
      return item;
    }
  }
  return null;
}

// Loop to prompt for name and then perform the searchStudent function to retrieve the student.
// The response then gets used to add to the html variable that is then written in the document
while (searchName !== 'quit') {
  searchName = prompt('Search for a students name [E.g. marc] to retreive their data or type [quit] to exit');
  let student = searchStudent(searchName, students); //student object of students array or null
   if (student) {  
      html += '<h2><b>Name: ' + student.name + '</b></h2>';
      html += '<p>Track: ' + student.track + '</p>';
      html += '<p>Achievements: ' + student.achievements + '</p>';
      html += '<p>Points: ' + student.points + '</p>';
   } 
} 

document.write(html);```