├── index.html
└── src
└── js
└── index.js
/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
7 |
8 | HW14-js-oop-classes
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/src/js/index.js:
--------------------------------------------------------------------------------
1 | class ToDoList {
2 | constructor() {
3 | this.todos = []
4 | }
5 | addTodo(task){
6 | this.todos.push(task)
7 | }
8 | removeTodo(id){
9 | this.todos = this.todos.filter((task) => task.id !== id)
10 | }
11 | showCompletedTasks () {
12 | return this.todos.filter((task) => task.status === true)
13 | }
14 | showInProgressTask () {
15 | return this.todos.filter((task) => task.status === false)
16 | }
17 | changeStatus(id, status) {
18 | let currentTask = this.todos.find(task => task.id === id)
19 | currentTask && (currentTask.status = status)
20 | return currentTask
21 | }
22 | findTasks(val) {
23 | return this.todos.filter(el => el.task.includes(val))
24 | }
25 | moveUp(task) {
26 | let currentIndex = this.todos.findIndex((el) => el.id === task.id)
27 | this.todos[currentIndex] = this.todos.splice(0, 1,this.todos[currentIndex])[0]
28 | return this.todos
29 | }
30 | moveDown(task) {
31 | let currentIndex = this.todos.findIndex((el) => el.id === task.id)
32 | this.todos[currentIndex] = this.todos.splice(this.todos.length-1, 1,this.todos[currentIndex])[0]
33 | return this.todos
34 | }
35 | }
36 |
37 | let toDoList = new ToDoList()
38 |
39 | class Task {
40 | constructor(task, status, id){
41 | this.task = task
42 | this.status = status
43 | this.id = id
44 | }
45 | }
46 | let task1 = new Task('to buy milk', true, 'task1')
47 | let task2 = new Task('to buy bread', true, 'task2')
48 | let task3 = new Task('to buy salt', false, 'task3')
49 | let task4 = new Task('to buy sugar', false, 'task4')
50 |
51 | //add tasks
52 | toDoList.addTodo(task1)
53 | toDoList.addTodo(task2)
54 | toDoList.addTodo(task3)
55 | toDoList.addTodo(task4)
56 |
57 | //remove task by id
58 | toDoList.removeTodo(task2.id)
59 | //show tasks with status true
60 | console.log(toDoList.showCompletedTasks());
61 | //show tasks with status false
62 | console.log(toDoList.showInProgressTask());
63 | //find task by id and change status
64 | console.log(toDoList.changeStatus('task3',true));
65 | console.log(toDoList.todos)
66 | //find task by value
67 | console.log(toDoList.findTasks('milk'))
68 | //find task and move up in te list
69 | console.log(toDoList.moveUp(task4))
70 | //find task and move down in te list
71 | console.log(toDoList.moveDown(task1))
72 |
--------------------------------------------------------------------------------