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 trialDan MacDonagh
4,615 PointsMy solution
Here's my solution to the problem:
var student;
var found = false;
function print(message) {
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = message;
}
while( true ){
search = prompt('Search for a student name, type quit to exit');
if (search === 'quit'){
break;
}
for(i = 0; i < students.length; i++){
if(search.toUpperCase() === students[i].name.toUpperCase()){
student = students[i];
message = '<p>Student name: ' + student.name + '</p>';
message += '<p>Track: ' + student.track + '</p>';
message += '<p>Points: ' + student.points + '</p>';
message += '<p>Achievements: ' + student.achievements + '</p>';
print(message);
break;
} else {
message = 'Didnt find it';
print(message);
}
}
}
1 Answer
Raj Rathore
9,728 PointsMy Answer
var message = '';
var foundStudents = [];
function print(message) {
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = message;
}
function getStudentReport(student) {
var report = '<h2>Student: ' + student.name + '</h2>';
report += '<p>Track: ' + student.track + '</p>';
report += '<p>Points: ' + student.points + '</p>';
report += '<p>Achievements: ' + student.achievements + '</p>';
return report;
}
function searchStudent(searchTerm) {
for (var i = 0; i < students.length; i += 1) {
student = students[i];
if(student.name.toLowerCase() === searchTerm.toLowerCase() ) {
foundStudents.push(student);
}
}
return foundStudents;
}
while (true) {
var searchTerm = prompt('Enter the name of the student you want to search?');
if (searchTerm.toLowerCase() === 'quit' || searchTerm === null ) {
break;
} else {
var student = searchStudent(searchTerm);
if (foundStudents.length > 0) {
for (var j = 0; j < foundStudents.length; j++) {
message += getStudentReport(foundStudents[j]);
}
} else {
message = "<h2> Student named " + searchTerm + " not found! </h2>";
}
}
print(message);
foundStudents = [];
message = '';
}
if(message === '') {
print("Thanks. Refresh page to get started again!");
}