-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
49 lines (47 loc) · 1.5 KB
/
script.js
File metadata and controls
49 lines (47 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const tasks = [];
//[1,2,3], [{task:''},{task:'test'}]
//' abc '.trim() -> 'abc'
//string,number,boolean,undefind,null,array,object
function addTask(){
console.log('add task')
const taskInput = document.getElementById('taskInput');
const priorityInput = document.getElementById('priorityInput');
const task = taskInput.value.trim();
const priority =parseInt(priorityInput.value);
//不管是input,select,textarea,它们的value都是string, '1'转化成数字用parseInt
console.log('task',task)
if(!task){
alert('Please enter a task.');
return;
}
let importance = '';
if(priority <= 2){
importance = 'high'
}else if(priority === 3){
importance = 'medium'
}else {
importance = 'low'
}
const newTask = {
'task': task,
'priority':priority,
'importance':importance
}
tasks.push(newTask)
console.log('tasks',tasks)
taskInput.value = '';
renderTasks();
}
function renderTasks(){
const arr = ['a','b','c'];
console.log('arr length',arr.length)// max index = arr.length - 1
const taskList = document.getElementById('taskList');
taskList.innerHTML = '';
for(let i = 0; i < tasks.length; i++){
const item = tasks[i]//tasks[0]
const li = document.createElement('li');
li.className = `task ${item.importance}` //'task ' + item.importance
li.textContent =`${item.task} (Priority ${item.priority})`
taskList.appendChild(li)
}
}