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

Ezekiel dela Peña
Ezekiel dela Peña
6,231 Points

Here's how I solved mine with additional scenarios.

Here's how I did it on this exercise. I had gone to a different approach from Dave.

Scope:

  • non-case sensitive search
  • returns nearest point of search (e.g. searches for Ezekiel instead of Ezekiel Dela Peña will return object with name property Ezekiel Dela Peña) [my code has a downside though]
  • returns a student not found.
function print(message) {
  return document.getElementById("output").innerHTML = message;
}

var html = "";
var search = prompt("Enter student name [e.g: Ezekiel] : ").toLowerCase();


var students = [
  {
    name : "Ezekiel Dela Peña",
    track : "Android Development",
    achievements : 23,
    points : 4294
  },
  {
    name : "Mary Madaloiuse",
    track : "Web Design",
    achievements : 27,
    points : 5523
  },
  {
    name : "Fhrey Aston",
    track : "IOS Development",
    achievements : 13,
    points : 1032
  },
  {
    name : "Jarren Quatz",
    track : "Game Development",
    achievements : 60,
    points : 10563
  },
  {
    name : "Aishan Adolfo",
    track : "Front End Development",
    achievements : 3,
    points : 120
  },
];


for(var i = 0; i < students.length; i++) {
  // if student["name"] contains search value. Prints it's info. 
  // else continue searching
  if(students[i].name.toLowerCase().indexOf(search) != -1) {
    for(var key in students[i]) { 
      if(key == "name") {
        html += "<h3><strong>" + key + " : " + students[i][key] + "</strong></h3><br>";
      } 
      else {
        html += "<h3>" + key + " : " + students[i][key] + "</h3>";
      }

      if(key == "points") {
        html += "<br>";
      }
    }
    break;
  } 
  else if(i+1 >= students.length) {
    html += "<h2>Student " + search + " was not found in our end.</h2>";
  }
}

print(html);
Vic Mercier
Vic Mercier
3,276 Points

Maybe you could use a for loop and the push method to store all student name in an array: const names = []; for(let i = 0; i < students.length; i+=1){ names.push(student[i].name); } After you can do something like: if(names.indexOf(search) != -1){ //and after you may use a for in loop to prints all the properties of the student object } Put best answer if that respond to your question!