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 trialEmerson Gonzalez
Front End Web Development Techdegree Student 22,500 PointsSearch Challenge Solution
var message = '';
var student;
var search;
function print(message) {
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = message;
}
function getStudentReport(student){
var report = '';
for(var i = 0; i < student.length; i += 1){
report += '<h2>Student: ' + student[i].name + '</h2>';
report += '<p>Track: ' + student[i].track + '</p>';
report += '<p>Points: ' + student[i].points + '</p>';
report += '<p>Achievements: ' + student[i].achievements + '</p>';
}
return report;
}
while(true){
var studenRecord = [];
search = prompt('Search for student records.\n( Type "quit" to exit)');
if(search === null || search.toLowerCase() === 'quit'){
break;
}else{
print('<h4>' + search + ' Could Not Be Found!</h4>');
}
for (var i = 0; i < students.length; i += 1) {
student = students[i];
if(student.name.toLowerCase() === search.toLowerCase()){
studenRecord.push(student);
message = getStudentReport(studenRecord);
print(message);
}
}
}
2 Answers
Iain Simmons
Treehouse Moderator 32,305 PointsThis will print the 'could not be found' message before checking to see if there are students with a name matching the search term.
Instead of that else
, change it to another if
after the for
loop that checks if the length of studenRecord
is 0.
Love Brynge
12,635 PointsWhy is the for-loop repeated in getSTudentReport-function? it seems to work but it doesnt make sense to me. To me it looks like the student already have been chosen when its ben put into studentRecord in the while-loop. Why loop through the students again? or is it only looping through the studentRecord the second time?
Iain Simmons
Treehouse Moderator 32,305 PointsThe getStudentReport
function takes an array of students so that it can print multiple reports. So, it has to loop through the array and print the details of each student. The parameter should probably be renamed to students
.
The for
loop within the while
is to use the current search term and loop through the array of all the students to see which ones match. If a student's name matches, then it will add them to another, temporary array (studenRecord
in this case) and then pass that array to the getStudentReport
function to be printed to the screen.
Hope that clears things up!
Wesley Wright
26,793 PointsWesley Wright
26,793 PointsSo what do you need help with exactly?