Skip to content
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

Learned how to wrap different functions and variables into an object … #7

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
56 changes: 36 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
@@ -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