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 Accessing Object Properties

William Melago
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
William Melago
Front End Web Development Techdegree Graduate 19,446 Points

Accidently got this to work. HELP

I want to print out only the first names of the stundents when search === 'list'. i changed the var in the for loop to studentList and it worked but i don't know why haha.

var message = '';
var student;
var search;

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

function getStudentReport( student ) {
    var report = '<h2>Student: ' + student.name + '</h2>';
    report += '<p>Track: ' + student.track + '</p>';
    report += '<p>Achievements: ' + student.achievements + '</p>';
    report += '<p>Points: ' + student.points + '</p>';
    return report;
}

function listOfStudents() {
  for(var i = 0; i < students.length; i++)
    var studentList = student[i]; //why does changing this var to studentList make it work?
    message += '<p> ' + student.name + ' </p>';
  return message;

}




while (true) {
  search = prompt('Search student records by typing name [Jody] (or type "quit" to end)');
  if (search === null || search.toLowerCase() === 'quit' ){
    break;
  } 
  search = search.toLowerCase();


    for (var i = 0; i < students.length; i++) {
    student = students[i];
      if (student.name.toLowerCase() === search) {
        message = getStudentReport( student );
        print(message);
      } else if ( search.toLowerCase() === 'list') {
        message = listOfStudents();
        print( message );
      }

      else if ( search.indexOf() === -1 && search !== 'list' ) {
       message = '<p>Sorry ' + search + ' is not a student</p>';
        print(message);
      }


  }
}



print(message);

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

The big question is what was studentList before you changed it? :) I'm of the opinion that you could remove that entire line and it'd still work!

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hmmm. Interesting. Well being that student is a global variable here and studentList is limited to that function I'd say the mystery lies in there somewhere. But being that I can't see what's in student, or students it's hard to say. However, I'm glad you got it worked out... even if it was on accident!

Kevin Goudswaard
Kevin Goudswaard
11,061 Points

First, you define a new variable in your function. You say var studentList = student[i];

so you have the globally defined variable known as "student" and then you say that "studentList" === "student"

because "student" is a global variable, you are simply copying it, and putting that copy into studentList. All this is done, by the way, inside the function. Because of that, studentList is in a local scope. So when you go to manipulate variable "student" (a global variable) later in your code, it is unaffected by "studentList"

so in short, your code works because "students" isn't affected by your function, and "studentList" never ended up doing anything in the code.

I am pretty sure that this is why. thoughts?