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 trialSebastian Bausch
7,028 PointsStudent record challenge super simple solution
Hi, my solution seems to be more concise than what I've seen so far. All is taken care within the while loop. Is that a good thing? Would love to hear your comments and suggestions. Thanks!
var html = '';
var search;
var student;
function print(message) {
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = message;
}
function getStudentReport (student) {
var report = '<h1>Student: ' + students[i].name + '</h1>';
report += '<p>Track: ' + students[i].track + '</p>';
report += '<p>Achievements: ' + students[i].achievments + '</p>';
report += '<p>Points: ' + students[i].points + '</p>';
return report;
}
while (true) {
search = prompt('Enter student name (or type "quit" to end)');
if (search === null || search.toLowerCase() === 'quit') {
break;
}
for (var i = 0; i < students.length; i++) {
student = students[i];
if (student.name.toLowerCase() === search.toLowerCase()) {
html += getStudentReport(student);
} else if (i == students.length - 1) {
html += 'Not found';
}
}
print(html);
html = '';
}
1 Answer
Victor Learned
6,976 PointsIt works but I'd make a slight change around your for loop to ensure that you always pass something back. I find it's always better practice in programming to assume false/not found until proven otherwise:
var result = "Not Found";
for (var i = 0; i < students.length; i++) {
student = students[i];
if (student.name.toLowerCase() === search.toLowerCase()) {
result = getStudentReport(student);
}
}
html+= result;
print(html);
html = '';