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 trialRafael Capati
10,725 PointsMy solution to the final challenge
Hey guys, what you can say about my code? It's pretty straight, but seems to be working.
var message = ""; // The final message to display
var student; // The actual student
var choice; // Store the user input
// Print the message into the DOM
function print(text) {
var outputDiv = document.getElementById("output");
outputDiv.innerHTML = text;
}
// Loop through the objects of students (matches found)
function getStudentReport(studentFound) {
var report = "";
for (var i = 0; i < studentFound.length; i++) {
report += "<h2>Student: " + studentFound[i].name + "</h2>";
report += "<p>Track: " + studentFound[i].track + "</p>";
report += "<p>Badges: " + studentFound[i].badges + "</p>";
report += "<p>Points: " + studentFound[i].points + "</p>";
}
return report;
}
while(true) {
choice = prompt("Type the student name. or \"quit\" to close.");
if (choice === null || choice.toLowerCase() === "quit") {
break;
} else {
choice = choice.toLowerCase();
var contain = false; // The choice is really a student name?
var found = []; // Array to store the matches found
for (var i = 0; i < students.length; i++) {
student = students[i];
if (student.name.toLowerCase() === choice) {
contain = true; // A student was found
found.push(student); // Add the student (object) to the array of matches
}
}
if (!contain) { // If no student was found...
alert("Could not find the student: " + choice);
} else {
// Otherwise, print the student information
message = getStudentReport(found);
print(message);
}
}
}
3 Answers
Iain Simmons
Treehouse Moderator 32,305 PointsWorks great! Just note that you don't necessarily need the variable contain
, you can just check if the length of the found
array is greater than 0.
My solution was similar:
var html = '';
function print(nodeID, message) {
document.getElementById(nodeID).innerHTML = message;
}
function buildDefinitionList(obj) {
var prop;
var dListHTML = '<dl>';
for (prop in obj) {
dListHTML += '<dt>' + prop.charAt(0).toUpperCase() + prop.slice(1) + '</dt>';
dListHTML += '<dd>' + obj[prop] + '</dd>';
}
dListHTML += '</dl>';
return dListHTML;
}
function buildStudentList(list) {
var i;
var uListHTML = '<ul>';
for (i = 0; i < list.length; i++) {
uListHTML += '<li>' + buildDefinitionList(list[i]) + '</li>';
}
uListHTML += '</ul>';
return uListHTML;
}
function getMatchingStudents(list) {
var query;
var i;
var resultList;
// keep looping until the user quits
while(true) {
// reset the result list
resultList = [];
// prompt and convert to lowercase
query = prompt("Search student records: type a name [Iain] (or type 'quit' to end)");
// check if they want to quit
if (query === null || query.toLowerCase() === 'quit') {
break;
}
// loop through students and check if the name contains the search query
for (i = 0; i < list.length; i++) {
if (list[i].name.toLowerCase().indexOf(query.toLowerCase()) > -1) {
// if so, push to the result list
resultList.push(list[i]);
}
}
// if there are results
if (resultList.length > 0) {
// build the list of matching students
html = buildStudentList(resultList);
} else {
// otherwise print a friendly not found message
html = '<p>Sorry, there are no students matching that name</p>';
}
// print html to output div
print('output', html);
}
}
getMatchingStudents(students);
Rishit Shah
4,975 PointsWorks fine. Were you able to figure out the code to sort the problem of two students having same name?. I am having a tough time figuring it out
Rafael Capati
10,725 PointsHey Rishit Shah, maybe I can help you. I edited my code with some comments, I think it's more clear now.
You just need to create a new array to add the matches found:
var found = [];
If a match was found, add the whole object to that array, using the push method:
found.push(student);
After that you have to loop through the objects in that array to display the information.
for (var i = 0; i < studentFound.length; i++) {
report += "<h2>Student: " + studentFound[i].name + "</h2>";
report += "<p>Track: " + studentFound[i].track + "</p>";
report += "<p>Badges: " + studentFound[i].badges + "</p>";
report += "<p>Points: " + studentFound[i].points + "</p>";
}
Rishit Shah
4,975 PointsGot it. Thanks a lot
Rafael Capati
10,725 PointsRafael Capati
10,725 PointsThanks to post your solution, variable contain you are dismissed!