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

Giovanni Martínez Castagnoli
Giovanni Martínez Castagnoli
6,464 Points

I solved the first step, but I need help with the matching students challenge, can someone look at my code!!

I solved everything except for the matching students record part, I created an array and called studentList, every time you search the student object is pushed into the array, and then I create a function that loops and return every item in the array, but for some reason when the array have multiple objects it only prints the last, I checked the console and the array hold both objects when names match., This is My code, meany thanks to all of you!

IMPORTANT FOR SOME REASON the information in loops is not displaying correctly in the post, here's is the line missing from the code: It should read: for (l = 0; l < studentsArray.length; l +=1)

// The Array of Object
var students = [
    {name: 'Karen', 
     tracks: 'Data Bases, SQL, MYSQL', 
     achivements: 120, 
     points: 8
    },
    {name: 'Roberto', 
     tracks: 'Marketing, MBA', 
     achivements:100, 
     points: 10
    },
    {name: 'Diego', 
     tracks:'PHP, SQL, .NET, JS', 
     achivements:120, 
     points:11
    },
    {name: 'Ignacio', 
     tracks:'IOS, ANDROID', 
     achivements:130, 
     points:8
    },
    {name: 'Giovanni', 
     tracks:'HTML5, CSS3, BOOTSTRAP, JS, JQ', 
     achivements:125, 
     points:9
    },
  {name: 'Ignacio', 
     tracks:'IOS, .NET', 
     achivements:100, 
     points:7
    },
  {name: 'Roberto', 
     tracks: 'Project Manager, Art Direction', 
     achivements:90, 
     points: 10
    }
];

The second part:

//var definition
var message = ``;
var student;
var search;
// this array holds the students found
var studentsArray = [];

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

//Information for Print Function
function studentList(studentsArray) {
  for (i = 0; i < studentsArray.length; i +=1){
    var information = '<h2>Student: ' + studentsArray[i].name + '</h2>';
    information += '<p>Tracks: ' + studentsArray[i].tracks + '</p>';
    information += '<p>Points: ' + studentsArray[i].points + '</p>';
    information += '<p>Achievements: ' + studentsArray[i].achivements + '</p>';
  }
  return information;
};

//The Loop
do {
  search = prompt('Search Student Record: type a name, for example [Dave], to end type [quit]'); 
  if (search === null) {
    break;
  } else {
     message ='<h2>The Student Name: ' + search + ' is not part of our Students Records</h2>';
     print(message);
  }
  for (var i = 0; i < students.length; i += 1) {
    student = students[i];
    if (student.name.toLowerCase() === search.toLowerCase()){
      // push every student and matching student into studentsArray
      studentsArray.push(student);   
      message = studentList(studentsArray);    
      print(message);
      }
  }

} while (search.toLowerCase() != 'quit')
// Go!!!  

2 Answers

rydavim
rydavim
18,814 Points

Your code is not displaying properly in your post because you've accidentally used back-ticks instead of single quotes.

var message = 'back-ticks were here';  // change this to '' or "" - you don't want to use back-ticks here

You're only printing one student from the array because you're declaring the variable information inside the for loop. This means that each time you iterate through, you're resetting information to be equal to the new student only. You want to declare the variable outside the loop, and then add the students to it one by one.

//Information for Print Function
function studentList(studentsArray) {
  var information = ''; // added declaration outside the loop
  for (i = 0; i < studentsArray.length; i +=1){
    information += '<h2>Student: ' + studentsArray[i].name + '</h2>'; // removed declaration, changed to +=
    information += '<p>Tracks: ' + studentsArray[i].tracks + '</p>';
    information += '<p>Points: ' + studentsArray[i].points + '</p>';
    information += '<p>Achievements: ' + studentsArray[i].achivements + '</p>';
  }
  return information;
};

Hopefully that helps. Please let me know if you have any additional questions. Happy coding! :)

Giovanni Martínez Castagnoli
Giovanni Martínez Castagnoli
6,464 Points

Thanks rydavim, I also add a var update before the do while loop and it works perfect.!!!