Skip to content
Closed
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
.DS_Store
.vscode
**/.DS_Store
**/.DS_Store
.qodo
18 changes: 11 additions & 7 deletions Sprint-3/todo-list/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,29 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Title here</title>
<title>Todo list</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">

<link rel="stylesheet" href="style.css" />

</head>
<body>
<form>
<form id="todo-form">
<div>
<input type="text" placeholder="New todo..." />
<input type="text" placeholder="New todo..." id="todoInput" />
</div>
<div>
<button type="submit">Add Todo</button>
<div class="outer-button-countainer">
<button id="addButton" type="submit">Add Todo</button>
<button
type="button"
id="remove-all-completed"
class="btn btn-primary mb-3"
>
class="btn btn-primary mb-3">
Remove all completed
</button>
</div>
</form>
<div><ul id="todo-list"></ul><div>
<div><ul id="completed-todo-list"></ul><div>
<script src="script.js"></script>
</body>
</html>
127 changes: 114 additions & 13 deletions Sprint-3/todo-list/script.js
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';

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.


// 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);

Choose a reason for hiding this comment

The 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();
}

Choose a reason for hiding this comment

The 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();
51 changes: 51 additions & 0 deletions Sprint-3/todo-list/style.css
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;
}
Loading