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 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
229,005 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
229,005 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