so i'm a beginner here and am trying out to creating a web app based on a tutorial. Based on what I've learned, within an object, I can reference the object's property by using the code this.property-name
Based on the codes below that work, just wish to confirm my assumption that 'this.todos' was created as an object below via the codes this.todos.push ({ todoText: todoText, completed: false, });
and hence, I can reference this.todos.property-name, like this.todos.todoText and this.todos.completed anywhere within toDoList object?
I don't know where I'm messing this up though. Thanks a bunch folks.
var toDoList =
{
todos: [],
displayToDo: function()
{
if (this.todos.length ===0)
{
console.log ("your todos is empty")
}
else
{
console.log('My To Dos:');
for (var i=0; i<this.todos.length; i++)
{
if (this.todos[i].completed === true)
{
console.log ('(x)', this.todos[i].todoText);
}
else
{
console.log ('( )', this.todos[i].todoText);
}
}
}
},
addToDo: function(todoText)
{
this.todos.push
({
todoText: todoText,
completed: false,
});
this.displayToDo();
},
changeToDo: function(position, todoText)
{
this.todos[position].todoText = todoText;
this.displayToDo();
},
deleteToDo: function(position)
{
this.todos.splice(position, 1);
this.displayToDo();
},
toggleCompleted: function(position)
{
var todo = this.todos[position];
todo.completed = !todo.completed;
this.displayToDo();
},
toggleAll: function()
{
var totalTodos = this.todos.length;
var completedToDos = 0;
for (var i=0; i<totalTodos; i++)
{
if (this.todos[i].completed === true)
{
completedToDos++;
}
}
if (completedToDos === totalTodos)
{
for (var i=0; i<totalTodos; i++)
{
this.todos[i].completed = false;
}
}
else
{
for (var i=0; i<totalTodos; i++)
{
this.todos[i].completed = true;
}
}
this.displayToDo();
}
};