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
Sarah Flynn
7,434 PointsStudent Search challenge solution including additional functionality suggested in final video
Yes, this can and should be refactored, but it works. :)
var input = '';
var student;
var html = '';
var found = false;
var promptMessage = '';
function print(result) {
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = result;
};
function composeResult(student) {
var result = '<h4>' + student.name + '</h4>';
result += '<p>' + student.track + '</p>';
result += '<p>' + student.achievements + '</p>';
result += '<p>' + student.points + '</p>';
return result;
}
while(true) {
input = prompt(promptMessage + 'Enter a student name you are searching for or quit to exit');
if(input.toLowerCase() === 'quit' || input === null) {
break;
}
for(var i = 0; i < students.length; i++) {
student = students[i];
if(student.name === input) {
console.log('adding to result');
found = true;
html += composeResult(student);
print(html);
}
}
if(found) {
promptMessage = input + ' was found. ';
found = false;
} else {
promptMessage = input + ' was not found. ';
}
}