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
Charlotte Frates
Courses Plus Student 2,429 PointsI cant figure out whats wrong with my code. It wont print anything even a name does exist:
var message = '';
var student;
var search;
var foundStudents;
function print(message) {
//document.write(message);
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = message;
}
function getStudentReport(students) {
var report = '<h2>Student: ' + students.firstName + '</h2>';
report += '<p>Track: ' + students.track + '</p>';
report += '<p>Achievments: ' + students.achievments + '</p>';
report += '<p>Points: ' + students.points + '</p>';
return report;
}
// repeatedly opens a prompt dialogue to search student names and print out data
while(true){
search = prompt('Search a student record: type a name [Clair] (or type "quit" to end)');
if (search === null || search === 'quit'){
break;
}
//goes through array and access key value pairs
var foundStudents = [ ];
for (var i = 0; i < students.length; i+=1) {
student = students[i]; //students is the array
if (student.firstName.toLowerCase() === search.toLowerCase()){
foundStudents.push(student);
print(message);
}
}
if (foundStudents.length) {
var message = getStudentReport(foundStudents);
print(message);
} else {
alert(search.name + "is not on our records.");
}
}
2 Answers
Nick Wilson
5,060 PointsI'm not sure what you're trying to do here. But I can see a couple issues with the code. First off, your array doesn't have any values.
var foundStudents = [ need, values, here];
Second: Your loop isn't looping through the array because your not calling it with the right name.
for (var i = 0; i < foundStudents.length; I += 1) {
student = foundStudents[i]; //students is the array
if (student.firstName.toLowerCase() === search.toLowerCase()){
foundStudents.push(student);
print(message);
}
}
Third: students is undefined meaning it's not a declared variable anywhere in the code.
I am off work now lol, but this might help you figure it out.
Charlotte Frates
Courses Plus Student 2,429 Pointsthanks i got it
Nick Wilson
5,060 PointsWhat did you end up with? Just curious.