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

Please review my solution for this challenge

var message = ''; var student = ''; var search = ''; var noStudent = '';

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

function getStudentReport( student ) { var report = '<h2>Student: ' + student.name + '</h2>'; report += '<p>Track: ' + student.track + '</p>'; report += '<p>Points: ' + student.points + '</p>'; report += '<p>Achievements: ' + student.achievements + '</p>'; return report; }

while (true) { search = prompt('Search student records: type a name [Jody] (or type "quit" to end)'); if (search === null || search.toLowerCase() === 'quit') { break; } for (var i = 0; i < students.length; i +=1) { student = students[i]; if ( student.name === search ) { message = getStudentReport( student ); print(message); break; }else if (student.name != search ){ noStudent = "<p>No student with the name "+search+".</p>"; print (noStudent); break; } } }

Can you please put your code into ```

2 Answers

Loek Weijts
Loek Weijts
6,159 Points

Hi i did it this way. Seems to work.

// variables
var studentslist = [
  {
    naam: 'loek',
    functie: 'illustrator',
    leeftijd: 40
  },
  {
    naam: 'loek',
    functie: 'vormgever',
    leeftijd: 10
  },
  {
    naam: 'theo',
    functie: 'boekhouder',
    leeftijd: 34
  },
  {
    naam: 'chris',
    functie: 'schrijver',
    leeftijd: 43
  }
]

// selected array
var foundStudent = [];
var antwoord;
var student;

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

// keep asking until x is typed
zoek = window.prompt('search student, x for exit');

  // for looping trough studenten, pass it on to foundStudent
  for (var i=0; i<studentslist.length; i+=1) {

    if (zoek === (studentslist[i].naam)) {
      foundStudent.push(studentslist[i]);
        }
      }

  // printing out the foundStudent list
  for (var i=0; i<foundStudent.length; i+=1) {
    var uitdraai = '<li>' + foundStudent[i].naam + '</li>';
    uitdraai += '<li>' + foundStudent[i].functie + '</li>';
    uitdraai += '<li>' + foundStudent[i].leeftijd + '</li><br>';
    print(uitdraai);
  }

  print("Thank you, goodbye");

Could you post the students' data?