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

JS - Code is not printing out 1 student details - Unsure

Hi All,

I've been trying to solve this problem from this JavaScript video.

I've created a function called searchStudents that loops through the array of students. This function has two parameters, the studentname to search on, and emptystring which will return the matches student details.

However once I call my function, the emptstring should be populated (if it finds a matching student name), and then this should be printed via the Print function. However nothing happens. Any ideas? Theres no syntax errors in my code.

var message = '';
var student;
var input = '';
var emptystring = '';

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


function searchStudents(studentname,emptystring)
{

  for (var i = 0; i < students.length; i += 1) {
  student = students[i];
  message += '<h2>Student: ' + student.name + '</h2>';
  message += '<p>Track: ' + student.track + '</p>';
  message += '<p>Points: ' + student.points + '</p>';
  message += '<p>Achievements: ' + student.achievements + '</p>';


  if (studentname === student.name)
  {

    emptystring = message;
    //console.log("we got into function!  " + emptystring);
    return emptystring;
   // break;
  }

  }

}


while (input != "quit")
{

   emptystring = '';
   input = prompt("Search student records : type a name [Jody], (or type 'quit' to end)");




   if (input == "quit")
   {
     break;
   }
    else 
    {


      searchStudents(input,emptystring)
      //console.log("this should print " + emptystring);
      print(emptystring);

    }



}

(Note - the array called Students is in its own JS file)

Many thanks

1 Answer

Manish Giri
Manish Giri
16,266 Points

I think, the problem lies here -

 else 
    {


      searchStudents(input,emptystring)
      //console.log("this should print " + emptystring);
      print(emptystring);

    }
  1. This function returns a string which is the populated message. But you're not saving the return value anywhere - searchStudents(input,emptystring). So your function result is basically lost.
  2. When you get to the next line print(emptystring);, the value of emptyString is still "", as it was initialized here - emptystring = '';`.

So it prints nothing.

Thankyou. Im too used to C# way of things!