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
Ben Ragus
4,993 PointsQuestion with loops.
Hello. I am redoing this course to get a better understanding of javascript, and I don't quite understand the loop part. I know somebody already asked this, but I didn't understand the answer. Here is the snippet of code I want explained. Thank you.
for(var i = 0; i < incompleteTaskHolder.children.length; i++) {
bindTaskEvents(incompleteTaskHolder.children[i],taskCompleted);
console.log("Bind list item events");
}
for(var i = 0; i < completedTaskHolder.children.length; i++) {
bindTaskEvents(completedTaskHolder.children[i],taskIncomplete);
}
Ben Ragus
4,993 PointsYes, I do understand what this loop is doing and it going over the children, however I do not understand why the parameters in the function are there and what it does. Thank you for a quick response!
3 Answers
Ryan Field
Courses Plus Student 21,242 PointsThe loop is literally going through each of the incompleteTaskHolder and completedTaskHolder children and binding them with either taskCompleted or taskIncomplete.
So with the first loop, on the first pass through, i = 0, so it is going to the first child in the incompleteTaskHolder array, and binding taskCompleted. It continues to do this until the value for i reaches the number of children (e.g. four children means 0 through 3, so when i is 4, the loop terminates), and then stops.
Does that help?
Ben Ragus
4,993 PointsThank you for your response.
I think I am understanding this. Still confusing for me, but I'll get it.
If do understand, it's basically binding the answer to the loop to the parameters in the function?
Allison Davis
12,698 PointsHi Ben, I'm rewatching this section as well. This thread was very helpful for me: (https://teamtreehouse.com/forum/bindtaskevents)
My brain is still a little confused about this section, but as best I can explain it so far:
The function bindTaskEvents is defined earlier in the program with its parameters. In the section you excerpted, the function is called within the loop. The parameters are actually the arguments of the function at this point. The function cycles over each child element and first assesses how many there are within the incomplete and the completed taskholders (in the first and second loop,respectively), and then binds it to either taskCompleted or taskIncomplete.
Again, I'm still working this out, so if this is incorrect, anyone feel free to dive in and correct me.
Olga Kireeva
9,609 PointsHi Ben, as I understood you know how a loop works and you are asking about the function bindTaskEvents() inside the loop, particularly about its parameters. In order to explain "why the parameters in the function are there and what it does" we need to see the function code itself.
From the code that you posted, I can only assume that we are passing two parameters to the bindTaskEvents() function: first parameter is an element of an array and the second parameter (taskCompleted/Incompleted) looks like a variable. Probably, you've already assign some values to these variables before using them in the loop.
I would recommend go slowly through the whole code and check each line. You can also post a link to the topic (or name of the course/stage/lecture) that you are working on - this way you can get more help from the students.
Victor Rundbaken
14,037 PointsVictor Rundbaken
14,037 PointsCan you be a little more specific about what you don't understand so I can address the question?