From cc70969afab8b028974411c0b0a550a9a0fc844b Mon Sep 17 00:00:00 2001 From: Lawrence Chan Date: Thu, 1 Sep 2022 16:27:50 -0700 Subject: [PATCH] Learned how to wrap different functions and variables into an object as propertyies/state and methods --- index.js | 56 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index ae033426..2d525442 100644 --- a/index.js +++ b/index.js @@ -1,31 +1,47 @@ // Arrays to keep track of each task's state -const taskTitles = []; -const taskComplete = []; +// const taskTitles = []; +// const taskComplete = []; +// const taskDescriptions = []; // Create a new task by adding to the arrays // A new task will be created as incomplete -function newTask(title) { - taskTitles.push(title); - taskComplete.push(false); -} +const newTask = (title, description) => { + const task = { + title: title, + description: description, + complete: false, + logState: function() { + console.log(`${this.title} has ${this.complete ? "" : "not "}been completed.`) + }, + markCompleted: function() { + this.complete = true; + }, + }; + return task; +}; // Mark a task as complete by setting the task's status in the `taskComplete` array to `true` -function completeTask(taskIndex) { - taskComplete[taskIndex] = true; -} +// const completeTask = (task) => { +// task.complete = true; +// }; -// Print the state of a task to the console in a nice readable way -function logTaskState(taskIndex) { - const title = taskTitles[taskIndex]; - const complete = taskComplete[taskIndex]; - console.log(`${title} has${complete ? " " : " not "}been completed`); -} +// // Print the state of a task to the console in a nice readable way +// const logTaskState = (task) => { +// console.log(`${task.title} has${task.complete ? " " : " not "}been completed`); +// }; // DRIVER CODE BELOW -newTask("Clean Cat Litter"); // task 0 -newTask("Do Laundry"); // task 1 +const task1 = newTask("Clean Cat Litter", "Take all the 💩 out of the litter box"); +const task2 = newTask("Do laundry", "😨"); +const tasks = [task1, task2]; -logTaskState(0); // Clean Cat Litter has not been completed -completeTask(0); -logTaskState(0); // Clean Cat Litter has been completed +// console.log(tasks); + + +// newTask("Clean Cat Litter"); // task 0 +// newTask("Do Laundry"); // task 1 + +task1.logState(); // Clean Cat Litter has not been completed +task1.markCompleted(); +task1.logState(); // Clean Cat Litter has been completed