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

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,456 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);```