Skip to content

Update index.js #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 70 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,76 @@
// Arrays to keep track of each task's state
const taskTitles = [];
const taskComplete = [];

// 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);
}

// Mark a task as complete by setting the task's status in the `taskComplete` array to `true`
function completeTask(taskIndex) {
taskComplete[taskIndex] = true;
}
// why is this better.
// Encapsulation – Each task is an object with properties.
// Methods – Encapsulated functions inside each task object for better structure.
// Task List Object – Manages tasks efficiently.
// List All Tasks – Easily display all tasks at once.








// Task Factory Function

// 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`);



function createTask(title) {
return {
title: title,
complete: false,

// Method to mark task as complete
completeTask() {
this.complete = true;
},

// Method to log task state
logState() {
console.log(`${this.title} has${this.complete ? " " : " not "}been completed`);
}
};
}

// DRIVER CODE BELOW
// Task List Manager
const taskManager = {
tasks: [],

// Add a new task
addTask(title) {
const task = createTask(title);
this.tasks.push(task);
},

// Mark task as complete by index
completeTask(index) {
if (this.tasks[index]) {
this.tasks[index].completeTask();
}
},

// Log a specific task's state
logTaskState(index) {
if (this.tasks[index]) {
this.tasks[index].logState();
}
},

// List all tasks
listAllTasks() {
this.tasks.forEach(task => task.logState());
}
};



taskManager.addTask("Clean Cat Litter");
taskManager.addTask("Do Laundry");

newTask("Clean Cat Litter"); // task 0
newTask("Do Laundry"); // task 1
taskManager.logTaskState(0); // Clean Cat Litter has not been completed
taskManager.completeTask(0);
taskManager.logTaskState(0); // Clean Cat Litter has been completed

logTaskState(0); // Clean Cat Litter has not been completed
completeTask(0);
logTaskState(0); // Clean Cat Litter has been completed
taskManager.listAllTasks(); // Logs all tasks