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 trialCRIS ACOSTA
2,121 PointsHow is this code not working?
so this code below does the following:
- loops through the object array.
- prompt will keep appearing until user types 'quit' or clicks cancel button.
- prints only first student's property when first student's name is type in the prompt but doesn't do anything when other student's name is searched.
code:
var search;
var studentReport = '';
function print(message) {
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = message;
}
function studentRecord(student){
var report = '<h4>' + student.name + '</h4>';
report += '<p>'+ student.track + '</p>';
report += '<p>'+ student.Achievements + '</p>';
report += '<p>'+ student.points + '</p>';
return report;
}
for (i=0; i<students.length; i++){
search = prompt('Search student by typing student\'s name i.e. or type "quit" to quit.');
if(search.toLowerCase() === students[i].name.toLowerCase()){
studentReport += studentRecord(students[i]);
print(studentReport);
}else if(search === null || search.toLowerCase() === 'quit') break;
}
I know the solution has already been shown here but what I need to know is why my own solution doesn't work and why it only prints the first student's property in the DOM?
3 Answers
Steven Parker
231,248 PointsA typical solution would ask for the name to search, and then loop through the list of students checking for a match to show.
But this code loops through the list of students, and for each one asks for a name to match with only that one. So on the first question, it can only match the first student. Then on the second question, it can only match the second student, etc. If you keep typing names, you'll eventually run out of students and the program will stop without needing to enter "quit".
Also, the "print" program overwrites ("=") the output contents instead of appending to them ("+=") , so only the last match will be displayed when the program ends.
Ari Misha
19,323 PointsHello there! It is subtle but you are only prompting input only once, right? Hence, It only executes once when it matches the student in the stored students' sequence. Maybe you might want to wrap whole code in a while loop and prompt the user over and over until they say the contrary. And in that case, you can capture all the user inputs and push 'em to an Array or concatenate as a string, and then execute the whole code you just wrote. (P.S.: You shouldn't use the same variable or functions name as the names that are defined in the Global scope of Javascript)
CRIS ACOSTA
2,121 Pointsmuchos gracias!