Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects The Student Record Search Challenge

1 Answer

Simon Coates
Simon Coates
28,693 Points

I made a couple changes.

var message = '';
var student;
var searchField;

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

function getStudent(studente) {
  var record = '';
  if (studente.length > 0 ){
    for (var i = 0 ; i < studente.length ; i += 1) {
      student = studente[i];
        record += '<h2>Student: ' + student.name + '</h2>';
        record += '<p>Track: ' + student.track + '</p>';
        record += '<p>Points: ' + student.points + '</p>';
        record += '<p>Achievements: ' + student.achievements + '</p>';
  } else {
    record += 'Mi dispiace non abbiamo studenti che si chiamano ' + searchField;

function chi(nome) {
  var risultati = [];
  for (var i = 0; i < students.length; i += 1) {
    student = students[i];
    if (student.name.toLowerCase() === nome.toLowerCase()) {
    //getStudent(risultati); //changed this
  getStudent(risultati); //moved this

do {
  searchField = prompt('Inserisci un nome o digita "quit" per interrompere');
} while (searchField.toLowerCase() !== 'quit' )

You were calling getStudent(risultati); repeatedly, as it was inside a loop. It was overwriting outputDiv.innerHTML again and again. Apart from this, I came up with the same solution.

Simon Coates
Simon Coates
28,693 Points

The print method is concealing the flaw (it replaces content, rather than just adding). You can see the problem if you replace print with:

function print(message) {
 document.write( message);