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

can add and list items in the todo list and complete them #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions que_hacer.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>Que Hacer List</title>
<script type="text/javascript" src="que_hacer.js"></script>
</head>
<body>

</body>
</html>
86 changes: 86 additions & 0 deletions que_hacer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@

// Todo is a format that I want to use 10000000 times
var Todo = function(){
this.tasks = []
};

var Task = function(newTask) {
this.id = 0,
this.description = newTask,
this.completed = false
}

// set of functions that ALL instances of the Todo object will inherit
Task.prototype.complete = function() {
this.completed = true
}

Todo.prototype.add = function(newTask) {
var task = new Task(newTask)
this.tasks.push(task)
task.id = this.tasks.length
};

// outside the Todo object literal to avoid making a copy EACH time
// you have a new task

Todo.prototype.remove = function(deleteMe) {
for (var i=0; i<=this.tasks.length; i++) {
console.log(this.tasks[i])
console.log(deleteMe)
if (deleteMe === this.tasks[i]){
console.log("I just deleted: ");
this.tasks.splice(i,1);
}
}
}

// display each task in the instance's array
Todo.prototype.list = function () {
for (var i in this.tasks) {
console.log(this.tasks[i]);
}
}

// instance of our Todo
var groceryList = new Todo();
groceryList.add("chocolate")
groceryList.add("bread")
groceryList.add("peanut butter")
groceryList.list();

// expect to remove chocolate from the array
// groceryList.remove("chocolate")
// groceryList.remove("bread")
// groceryList.list();


var yourPantry = new Todo();
yourPantry.add("rice")
yourPantry.add("potatos")
yourPantry.list();


// go get the rice and check it off on the list which is stored as an array
//gets the 'rice' from the tasks array
var completeRice = yourPantry.tasks[0];
//expect to see the ID associated with rice
// completeRice.id
// completeRice.description
completeRice.complete();
console.log(completeRice)

// delete rice from the list
yourPantry.remove(completeRice)

// should return FALSE in console and it does!
// console.log("chocolate" !== groceryList.tasks[1])

var partyStuff = new Todo();



// another way to see what is going on with just addTask and just list
// Todo.prototype = {
// addTask: function() {},
// list: function() {},
33 changes: 30 additions & 3 deletions todo_list.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,37 @@
var newTodoList = function() {
// ???
NewTodoList = function() {
this.tasks = []
};

NewTodoList.prototype.add = function(item) {
this.tasks.push(item);
}

NewTodoList.prototype.list = function() {
NewObject = function(id, description) {
var printTasks = {
id: id,
description: description,
completed: false,
}
}
}


// Driver code


var todoList = newTodoList();
var todoList = new NewTodoList();

todoList.add('banana');
todoList.add('cheese');
console.log(todoList.tasks)
todoList.list();


// this creates another copy of newTodoList
// gives us access to the tasks array outside the object literal
// var cookies = new NewTodoList();
// cookies.add('chocolate', 30);

// console.log(cookies.tasks)