Skip to content
This repository was archived by the owner on Dec 18, 2024. It is now read-only.

NW6 | Sabella Fisseha | JS2 | To Do List | WEEK 3 #214

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
47 changes: 30 additions & 17 deletions week-3/todo-list/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,38 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Title here</title>
<title>Todo List app</title>
<link rel="stylesheet" href="style.css" />
<link
Copy link

@SpectacularLandscape SpectacularLandscape Jan 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the end I don't think you used a custom font, so this <link> tag can be removed. We want to make sure that the site loads quickly for users and doesn't download unnecessary files, so removing this is an easy performance win! 💯

rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css"
integrity="sha384-pzjw8z+ua/cF9eUROoMVW3lvJxWvRWKGVSaEa07p4z3fZO6biHqo5L2fBL5Kb5Z6"
crossorigin="anonymous"
/>
</head>
<body>
<form>
<div>
<input type="text" placeholder="New todo..." />
</div>
<div>
<button type="submit">Add Todo</button>
<button
type="button"
id="remove-all-completed"
class="btn btn-primary mb-3"
>
Remove all completed
</button>
</div>
</form>
<script src="script.js"></script>
<h1>To Do Lists</h1>
<section>
<ul id="todo-list"></ul>
<form>
<div>
<input type="text" placeholder="New todo..." />
</div>
<div>
<button type="submit">Add Todo</button>
<button
type="button"
id="remove-all-completed"
class="btn btn-primary mb-3"
>
Remove all completed
</button>
</div>
</form>
<script src="script.js"></script>
</section>
</body>
</html>



88 changes: 86 additions & 2 deletions week-3/todo-list/script.js

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this file, there's two large chunks of code that are very similar, inside the populateTodoList and addNewTodo functions. Generally, we don't want to re-use code (a concept called DRY - Don't Repeat Yourself). Repeating code can make it difficult to make a change in the future - it's nice to only have to update code in one place

As a starting point - maybe some of the repeated code could be extracted into its own function? Both seem to be creating a new todo element, so maybe a function called createTodo or similar would be a good place for the code

Original file line number Diff line number Diff line change
@@ -1,25 +1,109 @@
function populateTodoList(todos) {
let list = document.getElementById("todo-list");
todos.forEach((element) => {
const listing = document.createElement("li");
const rem = document.createElement("span");
const add = document.createElement("span");
rem.textContent = "🗑";
add.textContent = "✅";
listing.appendChild(rem);
listing.appendChild(add);
const taskText = document.createTextNode(element.task);
listing.appendChild(taskText);
list.appendChild(listing);
let clickCount = 0;


add.addEventListener("click", () => {
clickCount++;
let isEvenClick = clickCount % 2 === 0;


if (isEvenClick) {
listing.style.textDecoration = "none";
} else {
listing.style.textDecoration = "line-through";
}
});


rem.addEventListener("click", () => {
listing.remove();
});
});
// 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);


// 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) {
const inputTodo = document.querySelector("input");
const buttonTodo = document.querySelector("button");
buttonTodo.addEventListener("click", (event) => {
event.preventDefault();
const newList = document.createElement("li");
let list = document.getElementById("todo-list");
const tex = document.createTextNode(inputTodo.value);
inputTodo.value = "";
const rem = document.createElement("span");
const add = document.createElement("span");
rem.textContent = "🗑";
add.textContent = "✅";
newList.appendChild(rem);
newList.appendChild(add);
newList.append(tex);
list.appendChild(newList);


let clickCount = 0;


add.addEventListener("click", () => {
clickCount++;
let isEvenClick = clickCount % 2 === 0;


if (isEvenClick) {
newList.style.textDecoration = "none";
} else {
newList.style.textDecoration = "line-through";
}
});


rem.addEventListener("click", () => {
newList.remove();
});
});


// 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!
}

addNewTodo();
// 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() {
const del = document.getElementById("remove-all-completed");
let list = document.getElementById("todo-list");
del.addEventListener("click", () => {
list.textContent = "";
});
// Write your code here...
}
deleteAllCompletedTodos();



48 changes: 47 additions & 1 deletion week-3/todo-list/style.css
Original file line number Diff line number Diff line change
@@ -1 +1,47 @@

body {
background-color: aquamarine;


font-size: 2rem;
}
section {
background-color: beige;
width: 78%;
margin-right: 10%;
margin-top: 10%;
margin-left: 20%;
}
h1 {
text-align: center;
}
div {
background-color: beige;
}


li {
list-style-type: none;
}
span {
position: relative;
left: 350px;
cursor: pointer;
}
input {
padding: 0.5rem;
margin-left: 20%;
}
button {
padding: 0.5rem;
border-radius: 10px;
background-color: aquamarine;
margin-top: 30px;
margin-left: 15%;
margin-bottom: 10%;
}