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

JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects The Student Record Search Challenge Solution

Arjun Singh
Arjun Singh
2,457 Points

My solution

Please provide me any feedback for my code below:

var student; var search; 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;

}

while(true) { search=prompt("Search student records:type a name [Jody] or type 'quit' to end"); var message=""; var recordFound=false; if(search===null||search==="quit") break; if(search==="") { alert("Please don't submit an empty name. Re-enter the name at the next prompt."); continue; } for(var i=0;i<students.length;i++) { student = students[i]; if(search===student.name) { message+=getStudentReport(student); recordFound=true;

    }

}

if(recordFound===false)
{
    alert("No records found for the name: "+search+". Please click OK and re-enter name again" );

}
print(message);

}

huckleberry
huckleberry
14,636 Points

Gotta fix that code buddy lol.

Looks like you tried wrapping it in the backticks like you're supposed to but something went awry.

Cheers,

Huck - :sunglasses:

1 Answer

I think u can make this code a lot simpler...when i have time i will show u my way but for now i was able to alter your code a little and come up with this <blockquote>

var student;
var search;
function print(message) { 
var outputDiv = document.getElementById('output'); 
outputDiv.innerHTML = message; 
}

function getStudentReport(student) {
var report = 'Student: ' + student.name + '<br>'; 
report += 'Track: ' + student.track + '<br>'; 
report += 'Points: ' + student.points + '<br>'; 
report += 'Achievements: ' + student.achievements + '<br>'; 
return report;
}

while(true) {
search=prompt("Search student records:type a name [Jody] or type 'quit' to end"); 
var message=""; 
var recordFound=false; 
if(search.toLowerCase() === "quit"){ 
break;
}else if (search === "") {
alert("Please don't submit an empty name. Re-enter the name at the next prompt.");
continue;
}
for(var i=0;i<students.length;i++) { 
student = students[i];
if(search===student.name) {
message+=getStudentReport(student); 
recordFound=true;
}
}else {
alert("No records found for the name: "+search+". Please click OK and re-enter name again" );
}
print(message);
}

</blockquote> If this helped..please mark my answer as the best