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

Mark Plattner
Mark Plattner
2,082 Points

Question about the report variable in the getStudentReport function

The solution by Live Kizgozi https://teamtreehouse.com/community/the-student-record-search-challenge-my-solution-for-searching-multiple-names puts the print outside of the loop which fixes the duplicate student error.

My question is how the variable stores both results. I understand concatination. So the two objects concatinate, BUT in the function the variable is defined in the scope: var report = <h2>...why isn't the var overwritten since it isn't concatinated?

Here's the code in question:

function getStudentReport(student){

   var report = '<h2> Name: '+student.name + '</h2>';
   report += '<p>Track: '+student.track + '</p>';
   report += '<p>Achievement: '+student.achievements + '</p>';
   report += '<p>Points: '+student.points + '</p>';

   return report;
} 

while ( true ) {

    message = '';
    search = prompt('To search, enter student name below or type \'quit\' to close the prompt');
    if ( search === null || search.toUpperCase() === 'QUIT' ){
      break;
    }

    for ( var i = 0; i < students.length; i += 1 ) {
      student = students[i];
      if ( student.name.toUpperCase() === search.toUpperCase() ) {
           message += getStudentReport( student );

      }

    }

    if(message === ''){
      message = 'No records matching student name: '+search;
    }
    print(message);

Thanks!

fixed code formatting

3 Answers

Mark Plattner
Mark Plattner
2,082 Points

Thanks. What was wrong with it?

If the code isn't wrapped with 3 backticks then it can be sometimes hard to read and won't paste in here in the same format as it might look in your text editor.

This post has more information about it: https://teamtreehouse.com/forum/posting-code-to-the-forum

Hi Mark,

Let's say there's going to be 2 student matches.

In the for loop, when you get to the first match it's going to call the getStudentReport() function and that will build up all the html for that student and return it.

So it gets returned and then added on to the message variable which is an empty string at this point. Then some more looping occurs and eventually the 2nd student is found. The report function is called once more and it builds up the html for the second student. It then returns it and it's added onto the message variable which currently contains the first student.

At this point, the message variable contains all the html for both student matches.

I'm not sure in your question if you were thinking that the report variable is storing multiple matches but it's really only a temporary variable. Once that function returns, the report variable no longer exists.

Let me know if that was what you were asking.

Mark Plattner
Mark Plattner
2,082 Points

Got it. Concats message not report. Thank you very much