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

printing multiple objects with the same name

var message = '';
var student;
while(true){
  var search=prompt('SEARCH NAME HERE');
  function print(message) {
    var outputDiv = document.getElementById('output');
    outputDiv.innerHTML = message;
  }
  if(search=== null || search.toLowerCase()==='quit'){
    break;

  }
  n=0;


  for (var i = 0; i < students.length; i += 1) {
    student = students[i];
    if(student.name===search.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>';



}



  }

}
print(message);

1 Answer

Steven Parker
Steven Parker
229,608 Points

If by "objects with the same name" you mean "objects that are contained in an array", then using the index as you have done here is the right way to identify them. Are you having trouble with it?

One possible trouble unrelated to printing that caught my eye is where you convert the search term to upper case. You don't show your data here, but unless you are storing all the names in upper case then nothing you type in will match any.

If you still have trouble, please explain the issue in more detail and be sure to show the complete code (or make a snapshot of your workspace and post the link to it here).