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

January Johnson
January Johnson
23,855 Points

My answer to the Student Record Search Challenge -- again...

Okay, again this is my answer to the challenge which is different from Dave's and of course could be improved upon but I think it satisfies the requirements. What do you guys think?

You can find my code here http://codepen.io/jjansa/pen/zGbzxP.

Sorry, didn't feel like figuring out the markdown for code in the forum right now. :)

1 Answer

I like it! Good job!

I like that you can return more than one result if there is more than one person with a name matching the query.

One thing to note, you should add the closing li tag after you add the value of the property. Oh and on that note, maybe change the name of the property, since it's not really 'aStudent' within 'thisStudent', it's a property of that student.

You're also only adding li tags to the HTML, when they should be grouped into separate lists. Try adding the opening and closing ul tags before and after the for..in loop.

Here's my take on this:

var html = '';
var query;


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 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)").toLowerCase();
    // check if they want to quit
    if (query === 'quit') {
      print('output', '<p>Thanks! Bye!</p>');
      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) > -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);