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 JavaScript Loops, Arrays and Objects Tracking Data Using Objects The Student Record Search Challenge

Kayla Larson
Kayla Larson
1,222 Points

I'm stuck on my code. I see others using functions but don't know how to include that.

I pasted my code below (sorry I can't figure out how to do it the way the markdown cheat sheet says)

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

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



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>Achievements: ' + student.achievements + '</p>';
  message += '<p>Points: ' + student.points + '</p>';
}

while(true) {
  search = prompt('Type in a name you want to look for or click "quit" to exit');
  search = search.toLowerCase();
  if (search === null || search ==='quit') {
    break;
  } else {
    if (student.indexOf(message) > -1) {
    print(student[i].join(', '));
  } 

}

print(message);

3 Answers

Balazs Peak
Balazs Peak
46,077 Points

I'm not exactly too sure what you are trying to achieve here. Please provide more goal-oriented description.

After looking into your code, what I can tell is that you have a global variable named "student", yet, you are iterating a variable named "students", with an s at the end, in plural. This might cause unexpected results for you.

Keep coding!

Kayla Larson
Kayla Larson
1,222 Points

So sorry about that - I have two js files in my html - one that has the array/object and the student variable is linking the information together - if that makes sense. I am trying to create a search to pull the information on a name. I'm having trouble pulling that info. Is a function needed to do this? I will watch the next video to see how Dave does it but I wanted to try to figure it out on my own as well.

Balazs Peak
Balazs Peak
46,077 Points

Functions are structures of code which enables the contained code to be called from another place of code, most of the time, more than once. Using functions is not for "being able to do something", rather just doing it better - in a reusable fashion.

If you already have the student list declared in another file, then you should not declare it in this file, because you are overwriting it. Another thing: you can only use it in this file if both files are referenced from the same html. That brings them "together", in a same running environment, to put it that way. :) (Edit: the one containing the data variable should be referenced first, and the one you are using it in should be referenced afterwards! This will become second nature later on, but now maybe I'll save some headache for you.)

I'd suggest to think about things like this for a while, for example 20 minutes, but if you get stuck, just keep going with the material. Stuff will make more and more sense along the way and you will become an incredible professional within 1 year. The only rule is DO NOT STOP! If you are tired, slow down. Do less than an hour a day. Do just 10 minutes a day. Eventually, you will recover, and you will realise that you are doing hours once again. The only thing that matters is do not stop ever. You can slow down, but don't stop.

Keep coding!