Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Akeia Escobar
Akeia Escobar
1,957 Points

Can'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

You 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
Akeia Escobar
1,957 Points

Thank you. That worked!