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

khay sing
khay sing
6,806 Points

I don't understand how this works

var students = [ { Name: 'KhaySing', Track: 'Front End Developer', Achievements: 50, Points: 3605 }, { Name: 'Heng', Track: 'Back End Developer', Achievements: 100, Points: 4605 }, { Name: 'KhaySing', Track: 'IOS Developer', Achievements: 350, Points: 5605 }, { Name: 'Chet', Track: 'Web Designer', Achievements: 650, Points: 6505 }, { Name: 'Seyha', Track: 'Android Developer', Achievements: 550, Points: 8605 } ];

if(search.toUpperCase() === student.Name.toUpperCase()){
  message = getreport(student);
  message = getreport(student);
  print(message);
}

} }

after i put one more " message = getreport(student); " it shows every 'KhaySing' data even it has more than two. how does this work because i define only 2 variable.

1 Answer

Steven Parker
Steven Parker
231,248 Points

This code is incomplete. The creation and assignment of "search" is missing, as is the definition of the function "getreport". And I suspect that the code responsible for your issue is inside "getreport", unless there is some additional loop code also not shown.

Also, you have a duplicated line:

  message = getreport(student);
  message = getreport(student);

The second line completely replaces the contents of "message", so the first line has no effect and can be removed.

khay sing
khay sing
6,806 Points

var students = [ { Name: 'KhaySing', Track: 'Front End Developer', Achievements: 50, Points: 3605 }, { Name: 'Heng', Track: 'Back End Developer', Achievements: 100, Points: 4605 }, { Name: 'KhaySing', Track: 'IOS Developer', Achievements: 350, Points: 5605 }, { Name: 'KhaySing', Track: 'Web Designer', Achievements: 650, Points: 6505 }, { Name: 'KhaySing', Track: 'Android Developer', Achievements: 550, Points: 8605 } ];

var message= '' ; var student; var search; function print(message){ document.write(message); }

function getreport (student){ var report= '<h2> Student: ' + student.Name + '</h2>'; report += '<p> Track: ' + student.Track + '</h2>'; report += '<p> Achievements: ' + student.Achievements + '</h2>'; report += '<p> Points: ' + student.Points + '</h2>'; return report; }

while (true) { search = prompt('Search students record: type a name [Dara] or type' + " 'quit' " + 'to exit the program.'); if (search === null ||search.toLowerCase() === 'quit'){ break; }

for (var i=0; i <students.length ; i+=1){ student = students[i]; if(search.toUpperCase() === student.Name.toUpperCase()){ message = getreport(student); message = getreport(student); print(message); } } }

this is full code . i mean i try to figure out how to display more than 2 object which has Name:"KhaySing" in array and if i delete message = getreport(student); it will show only 1 object then i added more one line it works but i dont understand why it's work.

Steven Parker
Steven Parker
231,248 Points

I see exactly the same thing with one line or two:

Student: KhaySing
Track: Front End Developer
Achievements: 50
Points: 3605

Student: KhaySing
Track: IOS Developer
Achievements: 350
Points: 5605

Student: KhaySing
Track: Web Designer
Achievements: 650
Points: 6505

Student: KhaySing
Track: Android Developer
Achievements: 550
Points: 860

I just enter "khaysing" for the first prompt, and "quit" for the second. Note that because of how browsers work, you won't see the output until you enter "quit".

Also, you have some unbalanced HTML tags on a few lines, for example:

  report += "<p> Track: " + student.Track + "</h2>";   // <- the "/h2" should be "/p"