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

Rick de Jong
Rick de Jong
3,305 Points

My Solution (31 Lines) + The special challenge

Hello,

I have a nice post again with my solution to this challenge and the extra part where you had to also write out the people's names who are found 2 times in total..

Tell my what think of my code and if I could improve on it?

Students List :

var students = [ 
  { 
   name: 'Dave',
    track: 'Front End Development',
    achievements: 158,
    points: 14730
  },
  {
    name: 'Jordy',
    track: 'iOS Development with Swift',
    achievements: '175',
    points: '16375'
  },
  {
    name: 'Jordy',
    track: 'PHP Development',
    achievements: '55',
    points: '2025'
  },
  {
    name: 'John',
    track: 'Learn WordPress',
    achievements: '40',
    points: '1950'
  },
  {
    name: 'Trish',
    track: 'Rails Development',
    achievements: '5',
    points: '350'
  }
];

The final Code :

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

var message = "";
var student;
var studentNames = [];

while (true){
  var input = prompt("Search student records: type a name 'Jordy' or type 'quit' to exit"); 
  message = "";
  if (input.toLowerCase() == "quit" || input === "" ){
    break;   
  } else{
    for ( var i = 0; i < students.length; i++ ) {
      for ( var property in students[i] ) {
       studentNames.push(property.name);
      }
      student = students[i]
      studentName = student.name;
      if ( student.name == input ) {
        message += '<h2>Student: ' + student.name + '</h2>';
        message += '<p>Track: ' + student.track + '</p>';
        message += '<p>Points: ' + student.points + '</p>';
        message += '<p>Achievements: ' + student.achievements + '</p><hr>';
        print(message);
      }
    }
  }
}

2 Answers

Rick de Jong
Rick de Jong
3,305 Points

It does post the html i use the print function for that and also i only show the current name because otherways it would list up and look messy

Steven Parker
Steven Parker
231,007 Points

:point_right: You forgot to post the HTML

But other than that, I notice it keeps asking for names until you enter quit, but then it only displays the data for the last one entered.

If you're not going to display data for them all, you might want to auto-quit after the first one is entered.