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

Marcin Wójcik
Marcin Wójcik
9,760 Points

Solution to the last challenge from Student Record Search Challenge - it was about on how to display 2 objects

I wanted to post a solution on question from video - question was on how to print to browser 2 objects simultaneously in case those are hosting data for 2 students with the same names.

below is a code:

var message = ''; var studentA; var studentB; var searchStudent; var studentsArray = [];

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

function getStudentReport(student) { var 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; }

for (var i = 0; i < students.length; i += 1) { studentsArray.push (students[i].name); }

while (true) { searchStudent = prompt('Type name of the student you are looking for in data base or type quit for exiting searchengine'); if ( searchStudent === null || searchStudent.toLowerCase() === 'quit' ) { break; }

if ( studentsArray.indexOf(searchStudent) === -1 ) { print('<h2>Sorry we dont have a student called <b>' + searchStudent + '</b> in our community.</h2>'); }

for (var i = 0; i < students.length; i += 1) { studentA = students[i]; if (searchStudent === studentA.name) {
for (var j = 0; j < students.length; j += 1) { studentB = students[j]; if ( studentA !== studentB && studentA.name === studentB.name) { message = getStudentReport(studentA); message += getStudentReport(studentB); print(message); }
} }
}

for (var i = 0; i < students.length; i += 1) { studentA = students[i]; if ( searchStudent === studentA.name ) { message = getStudentReport(studentA); print(message);
} }
}

Marcin Wójcik
Marcin Wójcik
9,760 Points

sorry that will not work. please take a look at it in here - if you are interested.

Thanks for feedback on that.

https://codepen.io/mwojcikk/pen/YOXVVO

1 Answer

Steven Parker
Steven Parker
230,274 Points

Your first loop detects a duplicated name and displays both student records, but the second loop replaces that output with just the last student with that name.

Try removing the second loop (lines 83-89).