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 trialAkeia Escobar
1,957 PointsCan't get the results to print
I can't seem to get the searched name to print. There's no error in the console. I haven't looked at other people's code as I'm looking for a push in the right direction and not a solution. I also wasn't sure if I should leave the for in loop, but it didn't work when I took it out either.
My code:
let HTML= '';
let search;
//print function
function print(message) {
let outputDiv = document.getElementById('output');
outputDiv.innerHTML = message;
}
let students = [
{
name: 'Cayden',
track: 'C#',
achievements: 10,
points: '2123'
},
{
name: 'Akeia',
track: 'Full Stack Development',
achievements: 15,
points: '4253'
},
{
name: 'Yaya',
track: 'Front End Development',
achievements: 13,
points: '2435'
},
{
name: 'Jason',
track: 'Beginner Python',
achievements: 12,
points: '2321'
},
{
name: 'Ashley',
track: 'iOS',
achievements: '9',
points: '1324'
},
];
while (true) {
search = prompt('Search student records (ex: "James") or type quit to exit ').toLowerCase();
if (search === 'quit') {
break;
} else {
for (let i = 0; i < students.length; i += 1) {
if (search === students[i].name) {
for (studentData in students[i]) {
HTML += '<p>' + studentData + ': ' + students[i][studentData] + '</br>'
}
}
}
}
}
print(HTML);
1 Answer
KRIS NIKOLAISEN
54,972 PointsYou have converted search
to lowercase but your student names are title case. You would need to convert those as well in your comparison:
if (search === students[i].name.toLowerCase()) {
Also not due to browser behavior the results won't display on the page until you quit.
Akeia Escobar
1,957 PointsAkeia Escobar
1,957 PointsThank you. That worked!