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

Developmentn of functions seems backwards. Why is this order?

The biggest confusion with this project is the order of the functions. It seemed we developed the code from the end and worked backwards. Also, why do we pass random parameters into functions such as createNewTaskElements("some new tasks")?

/** THIS IS THE CORRENT FILE
 * Created by mikekyllo on 1/16/17.
 */
var taskInput = document.getElementById("new-task"); //new-task
var addButton = document.getElementsByTagName("button")[0]; //first button
var incompleteTaskHolder = document.getElementById("incomplete-tasks"); //incomplete-tasks
var completedTaskHolder = document.getElementById("completed-tasks"); //completed-tasks

//Add a new task
var createNewTaskElement = function(){
    var newTask = document.createElement("li");
    //input (checkbox)
    var checkBox = document.createElement("input"); //checkbox
    //label
    var label = document.createElement("label");
    //input(text)
    var editInput = document.createElement("input");
    //button.edit
    var editButton = document.createElement("button");
    //button.delete
    var deleteButton = document.createElement("button");
    //Each element needs modifying

    //Each element needs appending
    newTask.appendChild(checkBox);
    newTask.appendChild(label);
    newTask.appendChild(editInput);
    newTask.appendChild(editButton);
    newTask.appendChild(deleteButton);

    return newTask
}


// Add a new task
var addTask = function() {
    console.log("Add task...")

    var newTask = createNewTaskElement("Some new task");
    //Append newTask to incompleteTaskHolder
    incompleteTaskHolder.appendChild(newTask);
    bindTaskEvents(newTask, taskCompleted);
}

// Edit an existing task
var editTask = function() {
    console.log("Edit Task...");
}

// Delete a task
var deleteTask = function() {
    console.log("Delete task...");
}

// Mark a task as complete
var taskCompleted = function() {
    console.log("Task complete...");
    var newTask = this.parentNode;
    completedTaskHolder.appendChild(newTask);
    bindTaskEvents(newTask,taskIncomplete);
}

// Mark a task as incomplete
var taskIncomplete = function() {
    console.log("Task incomplete...");
    var newTask = this.parentNode;
    incompleteTaskHolder.appendChild(newTask);
    bindTaskEvents(newTask,taskCompleted);
}

var bindTaskEvents = function(taskListItem, checkBoxEventHandler){
    console.log("bind list item events");
//Select newTask's children
    var checkbox = taskListItem.querySelector("input[type=checkbox]");
    var editButton = taskListItem.querySelector("button.edit");
    var deleteButton = taskListItem.querySelector("button.delete");
//Bind editTask to editButton
    editButton.onclick = editTask;
//Bind deleteTask to deleteButton
    deleteButton.onclick = deleteTask;
//Bind checknboxEventHandler to checkbox
    checkbox.onchange = checkBoxEventHandler;
}


//set the click handler to the addTask function

addButton.onclick = addTask

//Cycle over incompleteTaskHolder ul list items
for(var i = 0; i < incompleteTaskHolder.children.length; i++) {
    bindTaskEvents(incompleteTaskHolder.children[i], taskCompleted);
}
//Cycle over completeTaskHolder ul list items
for(var i = 0; i < completedTaskHolder.children.length; i++) {
    bindTaskEvents(completedTaskHolder.children[i], taskIncomplete);
}

1 Answer

Steven Parker
Steven Parker
243,656 Points

You didn't provide a link to the course page you found this on, so some of this is a bit of a guess. But I notice that the createNewTaskElement function doesn't take any parameters, so perhaps the seemingly random parameter you see passed to it is intended as a kind of comment regarding why the function is being called.

And by "the order of the functions", if you mean the order in which they are declared in the code, that's not very important as long as they are all declared before they are used. Even that would not be important if they were being declared directly as functions instead of being assigned to variables, as actual function definitions are "hoisted", which means they can even be defined after they are referenced and still work.

If my guesses haven't addressed your questions completely, please describe what you mean by "working backwads" and provide a link the course page you are working with (or at least identify the course, stage, and video).