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

Jennifer Christie
Jennifer Christie
4,038 Points

Why do I have to define the variable outside of the loop?

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

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

print(studentList);

This is my code. I watched the video to see why only the last object in my array appeared, and this was the solution.

var studentList = '';

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

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

print(studentList);

My question is why do I have to declare the variable first in order for it to loop? Why didn't work when I declared it in the loop?

Jennifer Christie ,

studentList variable is outside of loop because here we want to concatenate every student details in studentList variable.If you will define studentList variable inside of a loop than every time you loop second time the value of studentList will be set as a empty value.

Jennifer Christie
Jennifer Christie
4,038 Points

Thank you for responding Robert.

2 Answers

Steven Parker
Steven Parker
216,135 Points

Declaring the variable before the loop allows you to add to it each time through the loop. Then when the loop finishes it will have all the values from every cycle.

If you declare it inside the loop instead, it will start over each time and when the loop is done it will contain only the values from the very last person.

So the variable studentList is declared locally in your code in that it only exists inside the loop. It also is reset everytime the loop runs. So if you want to continually add to the list it must be global. Also your print is outside the loop, so the studentlist variable doesn't exist out there. Was it throwing an error that said that studentlist was null or was it printing a variable with only the last student in the list? It should have been saying studentlist was null if I understand correctly.

Steven Parker
Steven Parker
216,135 Points

Variables declared with "var" are not restricted to block scope. But what you are thinking of would have been an issue if the variable had been declared using "let".

Jennifer Christie
Jennifer Christie
4,038 Points

There were no errors, it just didn't loop through. It only displayed the last person in the object.