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 Solution

ryan andrew
ryan andrew
5,450 Points

if loop in code not printing?

var message = "";
var search;
var report;
var students = [
   {name: 'ryan', age: 29, location: 'toronto'},
   {name: 'deku', age: 21, location: 'japan'},
   {name: 'jack', age: 31, location: 'vaughn'},
   {name: 'erwin', age: 29, location: 'markham'}
];

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

function getStudentList(students){
  var report = '<h2> Name: ' + student.name + '</h2>';
  report += '<p> Age: ' + student.age + '</p>';
  report += '<p> Location: ' + student.location + '</p>';
  return report;
}

while (true){
  search = prompt('Please enter a students name [ryan] or [quit] to exit program');
  if ( search === null || search.toLowerCase() === 'quit') {
    var message = '<p> See you next time! </p>';
    print(message);
    break;
  } 
  for (var i = 0; i < students.length; i += 1){
    var student = students[i];
    if (student.name === search) {
      var message = getStudentList();
      print(message);
    }
  }
}
Steven Parker
Steven Parker
221,328 Points

When posting code, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.

2 Answers

Steven Parker
Steven Parker
221,328 Points

I see three issues:

The "getStudentList"" function takes an argument named "students" (plural), but in the body of the function there are references to "student" (singular) which has not been defined.

Also, when function is called, no argument is passed to it.

Finally, each call to the "print" function replaces any and all text printed before. Try making it add to the output instead of replacing it:

  outputDiv.innerHTML += message;  // note: += instead of =
Vasanth Baskaran
Vasanth Baskaran
4,333 Points

Hi Ryan

The getStudentList( students ) function is expecting an parmeter, but in your code you haven't passed any arguments. Please try once by passing an argument in it.

In our case it is trying to call a function without parameter which doesn't exist.

Thanks