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

Saqib Ishfaq
Saqib Ishfaq
13,912 Points

i understand why we have to create function to output data! but my question is how do we know that

that we have to create " function getStudentReport ( sudent ) " here! like how do we know if it cant be done without creating a function! well i tried completing the challenge , without one and its partially ok but not 100%. so my point here is why and how do we know okkk here we need to create a function! hope it dont sound confusing wot m tryn ask here.... thanks alot for any help here to help me understand this as its bothering me so much

4 Answers

Steven Parker
Steven Parker
230,995 Points

There are several reasons for creating functions, some of the common ones are:

  • code reuse — functions let you avoid repeating the same code in different places
  • readability — functions can help organize large programs into parts that are easier to understand
  • encapsulation — functions can separate parts of the program from each other

There are very few times when you must write a function, but without them any non-trivial program would be much larger and harder to maintain than it needs to be.

Jesus Mendoza
Jesus Mendoza
23,289 Points

Every small piece of functionallity should be separated into its own function and that function should be named properly with a short name that describes what it does. This way you write clean and module code that results in better code!

That's my opinion!

Saqib Ishfaq
Saqib Ishfaq
13,912 Points

Thanks jesus mendoza and steven parker for the answers! i guess iam just too overwhelmed rite now after seeing the part 2 solution of the challenge as i cudnt fully complete it by myself :(

Saqib Ishfaq
Saqib Ishfaq
13,912 Points

ok just to clarify in my head, i guess we create "function getStudentRecord " here to put the looping data(dont know if its the right term here), but iam referring to the data which we print onto the webpage when we loop through the objects created in part 1st of the challenge! so when the function is called on each loop as students[i] only one student data is shown each time on to the web page according to each search put in!

and this is the code:

var students = [ 
  { 
   name: 'Dave',
    track: 'Front End Development',
    achievements: 158,
    points: 14730
  },
  {
    name: 'Jody',
    track: 'iOS Development with Swift',
    achievements: 175,
    points: 16375
  },
  {
    name: 'Jordan',
    track: 'PHP Development',
    achievements: 55,
    points: 2025
  },
  {
    name: 'John',
    track: 'Learn WordPress',
    achievements: 40,
    points: 1950
  },
  {
    name: 'Trish',
    track: 'Rails Development',
    achievements: 5,
    points: 350
  }
];
var message = '';
var student;
var search;
var studentRecord;

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

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

while (true){
search = prompt ('Search student records: type a name[Jody](or type "quit" to exit.)');
  if(search === null || search.toLowerCase() === 'quit'){
    break;
  }
  for (var i = 0; i < students.length; i +=1){
  student = students[i];
    if (search === student.name){

     print(getStudentRecord( student ));
    } 
  }
}
Steven Parker
Steven Parker
230,995 Points

Since these functions are used only once each, there's no code re-use here. So they are primarily examples of organization for readability.

Saqib Ishfaq
Saqib Ishfaq
13,912 Points

thanks.....got another issue here, i have noticed in the code teacher used // if ( student.name === search ){} // does it matter if we switch the two, as far as i understand it shuldnt. but it does not work for me on this code! Also why is it that he hasnt used " toLowerCase() " here while searching ,and types 1st letter as capital when showing if it works! it surely does like that. but it doesnt when we type small letters or all capital and when i use toLowerCase() it doesnt work at all

Steven Parker
Steven Parker
230,995 Points

I'm not sure what you mean by "switch the two", but if you want to make the search work in a case-insensitive way, you'll need to perform case conversion on both the search term and the student's name.

Saqib Ishfaq
Saqib Ishfaq
13,912 Points

ok got the answer, i typed "toLowerCase()" on both and it worked, i understand the reason coz its === so it was checking if its exactly equals to student.name which wasnt the case at earlier condition. so now it sets them exactly equal by using "toLowerCase()" (sorry was stuck on it for like an hour so had to ask, but got it in the end)