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 Interactive Web Pages with JavaScript Traversing and Manipulating the DOM with JavaScript Perform: Traversing Elements with querySelector

13,125 Points

Can someone explain to me the arguments in this for loop?

I am confused with what the two parameters that are passed in the bindTaskEvents function are used for.

My second confusion is why is the last parameter in the logic of each for loop the opposite of what the for loop is checking. For instance, the first for loop is checking for incompleteTasksHolder but the last parameter passed in the logic is 'taskCompleted' and vice versa in the second for loop.

var bindTaskEvents = function(taskListItem, checkboxEventHandler){
    console.log('binding happening');
    //select it's children
    var checkBox = ;
    var editButton = ;
    var deleteButton = ;
        //bind the editTask to edit button
        //bind the deleteTask to the delete button
        //bind checkboxEventHandler to the checkbox

//Set the click handler to the addTask function. So when this element has been clicked, it will then do the addTask function. Not the other way around.
addButton.onclick = addTask;

//cycle over incompleteTaskHolder ul list items
for(var i; i < incompleteTasksHolder.children.length; i++){
        //bind events to list items children (taskCompleted)
        bindTaskEvents(incompleteTasksHolder.children[i], taskCompleted);

//cycle over complete completedTaskHolder ul list items
    for (var i; i < completedTasksHolder.children.length; i++){
        //bind events to list item's children (taskIncompleted)
        bindTaskEvents(completedTasksHolder.children[i], taskIncomplete);

2 Answers

The first parameter we are passing to the bindTaskEvents function, incompleteTasksHolder.children[i] is telling the function which child element of the list we are wanting to do something with. We have the two undordered-lists, 'incompleteTasksHolder' and 'completedTasksHolder'. These unordered-list elements have children that are list items, inputs, labels, and buttons. These children are given to us as an element collection, contained in an array corresponding to the unordered-list they are in. So, we can use exampleListName.children[index] to reference a particular child element of the list.

For the second parameter, we are using the for loops to pass in the opposite checkbox state to indicate the change we are making to the todo list item. For example, if an item is under the incomplete-tasks list, we want to have that item be removed from this and added to the completed-tasks list if we make a change to the checkbox. This is to give the user the functionality of marking an incomplete item as being completed. So, checking an incomplete item uses the taskCompleted function to mark that item as having been completed. Checking a complete item uses the taskIncomplete function to mark that item as incomplete. Basically, the second parameter is what we want to be done to the first parameter.

Oğulcan Girginc
Oğulcan Girginc
24,848 Points

Hi daniwao

If you learnt the answer for the first question you asked, do you mind sharing? Thanks!