generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 220
WM | ITP-JAN-25 | HATEF_EIDI | Module-Data-Groups | Sprint 3| Todo List #425
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| node_modules | ||
| .DS_Store | ||
| .vscode | ||
| **/.DS_Store | ||
| **/.DS_Store | ||
| .qodo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,126 @@ | ||
| function populateTodoList(todos) { | ||
| let list = document.getElementById("todo-list"); | ||
| // Write your code to create todo list elements with completed and delete buttons here, all todos should display inside the "todo-list" element. | ||
| } | ||
|
|
||
| // These are the same todos that currently display in the HTML | ||
| // You will want to remove the ones in the current HTML after you have created them using JavaScript | ||
| let todos = [ | ||
| { task: "Wash the dishes", completed: false }, | ||
| { task: "Do the shopping", completed: false }, | ||
| ]; | ||
|
|
||
| populateTodoList(todos); | ||
| let done = []; | ||
|
|
||
| function populateTodoList() { | ||
| const todoList = document.getElementById("todo-list"); | ||
| const completedList = document.getElementById("completed-todo-list"); | ||
|
|
||
| // Clearing the existing list items | ||
| todoList.innerHTML = ""; | ||
| completedList.innerHTML = ""; | ||
|
|
||
| // Function to create list items | ||
| function createListItem(todo, index, isCompleted) { | ||
| const listItem = document.createElement("li"); | ||
| listItem.className = 'todo-item'; | ||
|
|
||
| // Creating the text node for the task | ||
| const taskText = document.createTextNode(todo.task); | ||
| listItem.appendChild(taskText); | ||
|
|
||
| // Creating Span container for the buttons | ||
| const buttonContainer = document.createElement("span"); | ||
| buttonContainer.className = 'inner-button-container'; | ||
|
|
||
| // Creating the complete/uncomplete button | ||
| const completeButton = document.createElement("button"); | ||
| completeButton.textContent = isCompleted ? 'Incomplete ❌' : 'Complete ✔️'; | ||
| completeButton.onclick = () => toggleComplete(index, isCompleted); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good way to use ternary operator |
||
| buttonContainer.appendChild(completeButton); | ||
|
|
||
| // Creating the delete button | ||
| const deleteButton = document.createElement("button"); | ||
| deleteButton.textContent = 'Delete 🗑️'; | ||
| deleteButton.onclick = () => deleteTask(index, isCompleted); | ||
| buttonContainer.appendChild(deleteButton); | ||
|
|
||
| // Append the button container to the list item | ||
| listItem.appendChild(buttonContainer); | ||
|
|
||
| return listItem; | ||
| } | ||
|
|
||
| // Populate the todo list with incomplete tasks | ||
| todos.forEach((todo, index) => { | ||
| const listItem = createListItem(todo, index, false); | ||
| todoList.appendChild(listItem); | ||
| }); | ||
|
|
||
| // Populate the completed list with completed tasks | ||
| done.forEach((todo, index) => { | ||
| const listItem = createListItem(todo, index, true); | ||
| completedList.appendChild(listItem); | ||
| }); | ||
| } | ||
| //Creating paragraph depending on the list | ||
| function createParagraph() { | ||
| const todoList = document.getElementById("todo-list"); | ||
| const completedList = document.getElementById("completed-todo-list"); | ||
|
|
||
| if (completedList.hasChildNodes()) { | ||
| const doneParagraph = document.createElement("p"); | ||
| doneParagraph.textContent = "Completed Tasks"; | ||
| completedList.insertBefore(doneParagraph, completedList.firstChild); | ||
| } | ||
|
|
||
| if (todoList.hasChildNodes()) { | ||
| const todoParagraph = document.createElement("p"); | ||
| todoParagraph.textContent = "To Do Tasks"; | ||
| todoList.insertBefore(todoParagraph, todoList.firstChild); | ||
| } | ||
| } | ||
| function toggleComplete(index, isCompleted) { | ||
| if (isCompleted) { | ||
| // Move from done to todos | ||
| const [task] = done.splice(index, 1); | ||
| task.completed = false; | ||
| todos.push(task); | ||
| } else { | ||
| // Move from todos to done | ||
| const [task] = todos.splice(index, 1); | ||
| task.completed = true; | ||
| done.push(task); | ||
| } | ||
| populateTodoList(); | ||
| createParagraph(); | ||
| } | ||
|
|
||
| function deleteTask(index, isCompleted) { | ||
| if (isCompleted) { | ||
| done.splice(index, 1); | ||
| } else { | ||
| todos.splice(index, 1); | ||
| } | ||
| populateTodoList(); | ||
| createParagraph(); | ||
| } | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Smooth way of handling toggle, and I also like the way you destructure object |
||
| // This function will take the value of the input field and add it as a new todo to the bottom of the todo list. These new todos will need the completed and delete buttons adding like normal. | ||
| function addNewTodo(event) { | ||
| // The code below prevents the page from refreshing when we click the 'Add Todo' button. | ||
| event.preventDefault(); | ||
| // Write your code here... and remember to reset the input field to be blank after creating a todo! | ||
|
|
||
| const taskInput = document.getElementById("todoInput"); | ||
| const task = taskInput.value.trim(); | ||
| if (task) { | ||
| todos.push({ task, completed: false }); | ||
| taskInput.value = ""; | ||
| populateTodoList(); | ||
| createParagraph(); | ||
| } | ||
| } | ||
|
|
||
| // Advanced challenge: Write a fucntion that checks the todos in the todo list and deletes the completed ones (we can check which ones are completed by seeing if they have the line-through styling applied or not). | ||
| function deleteAllCompletedTodos() { | ||
| // Write your code here... | ||
| done = []; | ||
| populateTodoList(); | ||
| createParagraph(); | ||
| } | ||
|
|
||
| document.getElementById('todo-form').addEventListener('submit', addNewTodo); | ||
| document.getElementById('remove-all-completed').addEventListener('click', deleteAllCompletedTodos); | ||
|
|
||
| // Initial population of the todo list | ||
| populateTodoList(); | ||
| createParagraph(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,52 @@ | ||
| #todo-form{ | ||
| display: flex; | ||
| flex-direction: column; | ||
| align-items: center; | ||
| gap: 10px; | ||
| width: 100%; | ||
| margin-top: 20px; | ||
| padding: 20px; | ||
| } | ||
|
|
||
| .body{ | ||
| text-align: center; | ||
| } | ||
|
|
||
| .outer-button-countainer{ | ||
| display: flex; | ||
| flex-direction: row; | ||
| align-items: center; | ||
| justify-content: space-around; | ||
| gap: 10px; | ||
| flex-wrap: wrap; | ||
| } | ||
| #todoInput{ | ||
| border: none; | ||
| padding: 10px; | ||
| width: 100%; | ||
| margin-top: 10px; | ||
| border: 1px solid #ccc; | ||
| border-radius: 5px; | ||
| box-shadow: 10px 10px 10px rgba(0, 0, 0, 0.1); | ||
| } | ||
| #todo-list, #completed-todo-list{ | ||
| display: flex; | ||
| flex-direction: column; | ||
| align-items: center; | ||
| gap: 10px; | ||
| width: 100%; | ||
| margin-top: 20px; | ||
| padding: 20px; | ||
|
|
||
| } | ||
| #todo-list span, #completed-todo-list span{ | ||
| display: flex; | ||
| margin: 10px; | ||
| flex-direction: row; | ||
| align-items: center; | ||
| justify-content: space-around; | ||
| gap: 10px; | ||
| } | ||
| #todo-list li{ | ||
| padding: 5px; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
like I said in reading-list project, please define your constants at the beginning of your script not inside of your functions.