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

yoav green
yoav green
8,611 Points

i'm getting only the last name info

that's my code. why am i getting only the last name out of my objects?

var students = [ { name : 'yoav', track: 'JAVA', Achievements : 15, points : 30}, { name : 'neta', track: 'CSS', Achievements : 9, points : 10}, { name : 'gidon', track: 'HTML', Achievements : 16, points : 200}, { name : 'chanan', track: 'Ruby', Achievements : 100, points : 1000}, { name : 'refael', track: 'Python', Achievements : 30, points : 500} ];

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

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

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

while (true) { search = prompt('Type a name of a student. type "quit" to end the program'); search = search.toLowerCase(); if (search === null || search === 'quit') { break; } for(var i = 0; i < students.length; i += 1) { student = students[i]; if (search = student.name) { message = getStudentRaport(student); print(message); } } }

Adam Beer
Adam Beer
11,314 Points

Code

Wrap your code with 3 backticks (```) on the line before and after. If you specify the language after the first set of backticks, that'll help us with syntax highlighting.

      ```html
      <p>This is code!</p>
      ```

6 Answers

yoav green
yoav green
8,611 Points
var students = [
  { name :  'yoav', track:  'JAVA', Achievements : 15,  points : 30},
  { name :  'neta', track:  'CSS', Achievements : 9, points : 10},
  { name :  'gidon', track:  'HTML', Achievements : 16, points : 200},
  { name :  'chanan', track:  'Ruby', Achievements : 100, points : 1000},
  { name :  'refael', track:  'Python', Achievements : 30, points : 500}
];

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

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

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

while (true) {
search = prompt('Type a name of a student. type "quit" to end the program');
search = search.toLowerCase();
if (search === null || search === 'quit') {
  break;
  }
  for(var i = 0; i < students.length; i += 1) {
      student = students[i];
      if (search = student.name) {
      message = getStudentRaport(student);
        print(message);
    }
}
}
yoav green
yoav green
8,611 Points

here you go :) and thanks for the tip

Adam Beer
Adam Beer
11,314 Points

I tried my old solution, but the same mistake. I tried fix your code but I can't. Sorry. I don't know what is the problem.

Brenda Butler
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brenda Butler
Front End Web Development Techdegree Graduate 18,839 Points

Hi Yoav. I am having the same issue. Initially I had the code written differently to where the HTML code for the 'message' variable was actually written into the for... loop and that printed all the results to the page in the order they were requested.

When I changed my code to match the one in the video I am getting the same results as you are. I think the issue might be that due to the fact that the print(message) command isn't written to the web page until we exit the loop, so essentially we are changing the value of the variable 'message' each time we enter the loop with a true if... statement. I'm still working on fixing this issue and the follow up challenges from the end of the video, but in case this helps I thought I would share my thoughts.

Good luck!