-
Notifications
You must be signed in to change notification settings - Fork 39
Ex. 1 - Rotem Ben David #14
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,24 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<title>Exercise 1</title> | ||
<title>TO-DO List</title> | ||
<link rel="stylesheet" href="style.css" /> | ||
</head> | ||
<body> | ||
|
||
<h1>Todo list</h1> | ||
|
||
|
||
<div class="container"> | ||
<h1>TO-DO list</h1> | ||
<div class="newTask"> | ||
<input type="text" placeholder="Add new TO-DO"> | ||
<button type="submit" class="add">+</button> | ||
</div> | ||
<div class="tasks"> | ||
</div> | ||
<div class="bottom"> | ||
<span class="howMany"></span> | ||
<button type="button" class="sort" style="display:none">sort</button> | ||
<button type="reset" class="clear" style="display:none">clear all</button> | ||
</div> | ||
</div> | ||
</body> | ||
<script src="script.js"></script> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
let input = document.querySelector(".newTask input"); | ||
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. It could also be a constant. |
||
const add = document.querySelector(".newTask button"); | ||
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. Again, you could have used a more specific name |
||
const tasksView = document.querySelector(".tasks"); | ||
const tasks = []; | ||
const howManyTasks = document.querySelector(".bottom .howMany"); | ||
const showClearAll = document.querySelector(".bottom .clear"); | ||
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.
|
||
const showSort = document.querySelector(".bottom .sort"); | ||
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.
|
||
|
||
//defult massege displayed when there are no tasks | ||
const defultMassage = 'Please add your TO-DO List!'; | ||
howManyTasks.innerHTML = defultMassage; | ||
|
||
//add new tasks | ||
add.addEventListener('click', () => { //recognize click on 'add' button | ||
addNewTask(input.value); | ||
}); | ||
|
||
input.addEventListener('keypress', (event) => { //recognize pressing enter key | ||
if (event.keyCode === 13) { | ||
event.preventDefault(); | ||
addNewTask(input.value); | ||
} | ||
}); | ||
|
||
function addNewTask(value) { | ||
if (value === '') { | ||
alert("You can't add an empty task"); | ||
} | ||
else { | ||
showTasks(value); | ||
updateBottom(); | ||
} | ||
} | ||
|
||
//show the tasks | ||
function showTasks(newTask) { //adds new task to tasks array and adds it to html | ||
let newLiTag = ''; | ||
if (newTask != undefined) { | ||
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. Lines 38-40 should have been part of
|
||
tasks.push(newTask); | ||
} | ||
tasks.forEach((element, index) => { | ||
newLiTag += '<li onClick="taskAlert('+index+')">'+element+'<span onClick="deleteTask('+index+');event.stopPropagation();">🗑</span></li>'; | ||
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. You called the variable Besides it, it is not clear what |
||
}); | ||
tasksView.innerHTML = newLiTag; | ||
input.value = ""; //clears the input after adding task | ||
} | ||
|
||
//updates the bottom | ||
function updateBottom() { //modify the amount of tasks displayed | ||
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. It's unclear what |
||
if (tasks.length >= 1) { | ||
let newSpan = ''; | ||
newSpan = '<span>You have '+tasks.length+' pending tasks</span>' | ||
howManyTasks.innerHTML = newSpan; | ||
if (tasks.length === 1) { //display clear all button only when there are tasks | ||
showClearAll.style = 'display: block'; | ||
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. Using classes is preferred 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. @or-nuri-monday can you please explain what do you mean? (I fixed all other commants) |
||
showSort.style = 'display: block'; | ||
} | ||
} | ||
else { //delete clear all button when there aren't tasks | ||
showClearAll.style = 'display: none'; | ||
showSort.style = 'display: none'; | ||
howManyTasks.innerHTML = defultMassage; | ||
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.
|
||
} | ||
} | ||
|
||
//delete task | ||
function deleteTask(index){ | ||
tasks.splice(index, 1); | ||
showTasks(); | ||
updateBottom(); | ||
} | ||
|
||
//clear all | ||
showClearAll.addEventListener('click', () => { //recognize click on 'clear all' button | ||
tasks.length = 0; | ||
showTasks(); | ||
updateBottom(); | ||
}); | ||
|
||
//task alert | ||
function taskAlert(index) { | ||
alert(tasks[index]); | ||
} | ||
|
||
//sort | ||
showSort.addEventListener('click', () => { //recognize click on 'sort' button | ||
tasks.sort(); | ||
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. Sort without a function is usually not recommended as it is unclear how the array is sorted. |
||
showTasks(); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
|
||
* { | ||
box-sizing: border-box; | ||
} | ||
|
||
body { | ||
height: 100vh; | ||
width: 100%; | ||
background: linear-gradient( to bottom, #f0fba6 0%, #fb5b2f 100%) | ||
} | ||
|
||
.container { | ||
margin: 120px auto; | ||
max-width: 50%; | ||
background-color: aliceblue; | ||
padding-inline: 25px; | ||
padding-top: 10px; | ||
padding-bottom: 25px; | ||
border-radius: 5px; | ||
} | ||
|
||
.container h1 { | ||
color: rgb(0, 0, 0); | ||
font-size: 30px; | ||
text-align: center; | ||
} | ||
|
||
|
||
.newTask input { | ||
width: 100%; | ||
min-width: 85%; | ||
height: 100%; | ||
border: 1px solid #ccc; | ||
font-size: 17px; | ||
border-radius: 3px; | ||
padding-left: 15px; | ||
outline: none; | ||
} | ||
|
||
.newTask button { | ||
width: 50px; | ||
height: 100%; | ||
border: none; | ||
outline: none; | ||
background: rgb(255, 162, 0); | ||
color: #fff; | ||
font-size: 22px; | ||
cursor: pointer; | ||
border-radius: 3px; | ||
margin-left: 5px; | ||
|
||
} | ||
|
||
.container .newTask { | ||
display: flex; | ||
height: 45px; | ||
width: 100%; | ||
margin: 20px 0; | ||
} | ||
|
||
.container .tasks { | ||
max-height: 250px; | ||
overflow-y: auto; | ||
} | ||
|
||
.tasks li { | ||
list-style: none; | ||
height: 45px; | ||
line-height: 45px; | ||
position: relative; | ||
background: #f2f2f2; | ||
border-radius: 3px; | ||
margin-bottom: 8px; | ||
padding: 0 15px; | ||
overflow: hidden; | ||
} | ||
|
||
.tasks li:hover { | ||
background: #d9d9d9; | ||
cursor: pointer; | ||
} | ||
|
||
.tasks li span { | ||
position: absolute; | ||
right: -45px; | ||
color: #fff; | ||
background-color: red; | ||
width: 45px; | ||
text-align: center; | ||
border-radius: 0 3px 3px 0; | ||
cursor: pointer; | ||
transition: all 0.3s ease; | ||
} | ||
|
||
.tasks li:hover span { | ||
right: 0px; | ||
} | ||
|
||
.container .bottom { | ||
display: flex; | ||
width: 100%; | ||
margin-top: 15px; | ||
padding-left: 15px; | ||
} | ||
|
||
.bottom button { | ||
border: none; | ||
outline: none; | ||
background: gray; | ||
color: #fff; | ||
font-size: 16px; | ||
cursor: pointer; | ||
border-radius: 3px; | ||
padding: 6px 10px; | ||
} | ||
|
||
.container .bottom .sort { | ||
margin-left: auto; | ||
margin-right: 5px; | ||
} |
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.
Using a class instead of an inline style attribute is always better. Only in specific cases, using an inline style is preferred.