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 trialAndy Birchwood
3,703 PointsWhere do I reset the message variable so that it only prints matches to current search, rather than appending new ones?
var message = '';
var matches = [];
var search;
function print(message) {
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = message;
message = '';
}
// store the data of matching students an array
function storeResult(searchMatch) {
matches.push(searchMatch);
// console.log(matches);
getStudentReport(matches)
}
// Append output for all objects in matches to message
function getStudentReport(matches) {
// loop through matches array and generate html
for ( var i = 0; i < matches.length ; i += 1 ) {
var match = matches[i];
console.log(matches[i]);
report = '<h2>Student: ' + match.name + '</h2>';
report += '<p>Track: ' + match.track + '</p>';
report += '<p>Achievements: ' + match.points + '</p>';
report += '<p>Points: ' + match.achievements + '</p>';
}
message += report;
// print all the results to the screen
print(message);
}
while (true) {
// reset matches array for every new search
matches = [];
// prompt user for search term
search = prompt("Which student do you want data for? Type 'quit' to exit");
// exit if user presses cancel or types quit
if ( search === null || search.toLowerCase() === 'quit' ) {
break;
}
// loop through students
for ( var i = 0; i < students.length ; i += 1 ) {
student = students[i];
// if you find a matching student, store it in matches
if ( search.toLowerCase() === student.name.toLowerCase() ) {
storeResult(students[i]);
}
}
}
rydavim
18,814 PointsNo worries, I've fixed the code formatting in your post. You can do this using the following markdown:
```language
your code here
```
3 Answers
rydavim
18,814 PointsInstead of resetting message
in your print function, try putting it at the end of your while
loop. That should do the trick if you only want to print the most recent search.
Andy Birchwood
3,703 PointsThanks rydavim , but I'm only getting one match at a time now... I need it to reset when a new search is performed, not each time the while loop discovers a match!
I just realised I posted this message outside the forum for the lesson... sorry it's this one https://teamtreehouse.com/library/javascript-loops-arrays-and-objects/tracking-data-using-objects/the-student-record-search-challenge-solution
Andy Birchwood
3,703 PointsI'm sorry, i think you were correct after all, it was my json that was the problem - there was only one match to be found!
Andy Birchwood
3,703 PointsAndy Birchwood
3,703 PointsSorry for the formatting. I couldn't figure out why it's doing this :(