js-mentorship-razvan / javascript

Javascript study notes
GNU General Public License v3.0
22 stars 2 forks source link

Improve todoApp application by adding a toggleAll method #388

Closed odv closed 4 years ago

odv commented 4 years ago
RazvanBugoi commented 4 years ago
let toDoApp = {
    todos: [],
    displayTodos: function() {
        if (this.todos.length == 0) {
            return 'My todos list is empty';
        } else {
            for (let i=0; i < this.todos.length; i++){
                console.log(this.todos[i].completed == true ? `(x) ${this.todos[i].todoText}` : `() ${this.todos[i].todoText}`);
            }
        }
    },
    addTodos: function(input) {
        this.todos.push({
            todoText: input,
            completed: false
        });
        this.displayTodos();
    },
    changeTodos: function(output, position) {
        this.todos[position].todoText = output;
        this.displayTodos();
    },
    toggleCompleted: function(pos) {
        this.todos[pos].completed = !this.todos[pos].completed;
        this.displayTodos();
    },
    toggleAll: function(){
        let taskNo = this.todos.length;
        let completedTasks = 0;
        for (let i=0; i<taskNo; i++) {
            if (this.todos[i].completed == true) {
                completedTasks += 1;
            }
        };
        for (let i=0; i<taskNo; i++) {
            if (completedTasks == taskNo) {
                this.todos[i].completed = false;
            } else {
                this.todos[i].completed = true;
            }
        }
         this.displayTodos();
    },
    deleteTodos: function(element) {
        this.todos.splice(element, 1);
        this.displayTodos();
    },
    deleteAllTodos: function() {
        this.todos = [];
        this.displayTodos();
    }

};