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

Amber Lim
Amber Lim
4,707 Points

My code does not print out anything after I tried to add the "print out student with same names" feature.

I created an empty array foundStudents to "push" the students with the same names. Nothing was printed onto the screen. I'd appreciate any pointers.

//establishing variables
var message = '';
var student;
var search;
var foundStudents = [];

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

function getStudentReport( student ){
  var report = '';
  for(var i = 0; i<student.length; i++){
  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;
}

//looping through students' information 
while(true){
 search = prompt("Type a student's name to access their records or type 'quit' to quit this search.");
  if( search === null || search === 'quit' ){
  break;
}

  for (var i = 0; i < students.length; i += 1){
  student = students[i];
    if(search.toLowerCase === student.name.toLowerCase()){
      foundStudents.push(student);
   }
 }
  if(foundStudents.length === 0){
    alert(student.name + "is not on our records.");
  } else {
     message = getStudentReport(foundStudents);
     print(message);
  }
}

Hey Amber Lim , Ok I caught some errors in your code

  if(search.toLowerCase === student.name.toLowerCase()){
      foundStudents.push(student);
   }

if(search.toLowerCase() === student.name.toLowerCase())

  for(var i = 0; i<student.length; i++){
  report += '<h2>Student: ' + student.name + '</h2>';
  report += '<p>Track: ' + student.track + '</p>';
  report += '<p>Points: ' + student.points + '</p>';
  report += '<p>Achievements: ' + student.achievements + '</p>';
  }

student[i].property

2 Answers

Sergey Podgornyy
Sergey Podgornyy
20,660 Points

Absolutely agree with Ashish, but I change your code according to JavaScript best practices:

// Fill test data about students
var students = [
    {
        name: 'Aaron',
        track: 'JavaScript',
        points: 500,
        achievements: []
    }, {
        name: 'Tom',
        track: 'PHP',
        points: 1500,
        achievements: []
    }, {
        name: 'Stewie',
        track: 'Ruby',
        points: 50,
        achievements: []
    }
];
var foundStudents = [];

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

function getStudentReport(students) {
    return students.reduce(function(sum, current) {
        return sum
            + '<h2>Student: ' + current.name + '</h2>'
            + '<p>Track: ' + current.track + '</p>'
            + '<p>Points: ' + current.points + '</p>'
            + '<p>Achievements: ' + current.achievements + '</p>';
    }, '');
}

// Searching for students
while (true) {
    var search = prompt("Type a student's name to access their records or type 'quit' to quit this search.");
    if (search === null || search === 'quit') {
        break;
    }

    students.forEach(function(item, index) {
        if (search.toLowerCase() === item.name.toLowerCase()) {
            foundStudents.push(item);
        }
    });

    if (foundStudents.length) {
        var message = getStudentReport(foundStudents);
        print(message);
    } else {
        alert(search.name + "is not on our records.");
    }
}

You could test it online here

Amber Lim
Amber Lim
4,707 Points

Thanks for the help, Sergey, and pointing out my errors. Really appreciate it. Your code is a lil' complex for my brain to handle right now. I hope one day I can read your code like I read a book. :')

Sergey Podgornyy
Sergey Podgornyy
20,660 Points

Amber, if you will practice every day, it will be very soon :wink: You just need to learn Callback topic

var foundStudents = []; var search;

alert( search + " is not on our records.");

Amber Lim
Amber Lim
4,707 Points

Ashish, by student[i].property what do you mean? Can you please elaborate? Perhaps leave comments in the actual code where this advice can be applied. Because I don't understand how student[i].property should be used in the code. Thank you so much!

Sergey Podgornyy
Sergey Podgornyy
20,660 Points

Hi Ashish, I'll answer for Ashish. Hope this is Ok?

In your code in function getStudentReport() inside for loop you wrote just:

for(var i = 0; i<student.length; i++){
  report += '<h2>Student: ' + student.name + '</h2>';
  report += '<p>Track: ' + student.track + '</p>';
  report += '<p>Points: ' + student.points + '</p>';
  report += '<p>Achievements: ' + student.achievements + '</p>';
  }

But in this case student is an array, and you should specify which element you wanna display, so you need to specify an index:

for(var i = 0; i<student.length; i++){
  report += '<h2>Student: ' + student[i].name + '</h2>'
         + '<p>Track: ' + student[i].track + '</p>'
         + '<p>Points: ' + student[i].points + '</p>'
         + '<p>Achievements: ' + student[i].achievements + '</p>';
  }

Same as you reassign here:

for (var i = 0; i < students.length; i += 1){
    student = students[i];    // <<== HERE
    if (search.toLowerCase() === student.name.toLowerCase()) {
         foundStudents.push(student);
    }
}
Amber Lim
Amber Lim
4,707 Points

Sergey! Thank you so freaking much for the encouragement and solution. It helped. Really appreciate it!