-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
169 lines (127 loc) · 5.58 KB
/
script.js
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//this is the counter variable for the number of tasks added
let count = 0;
let tasklist = document.getElementById('tasklist');
//load the tasks
function loadPreviousSessionTasks(){
//username of the user
const username = document.getElementById('Username');
username.innerText = localStorage.getItem('username');
let loadSession = localStorage.getItem('savedData');
if (loadSession){
//displaying on the page
tasklist.innerHTML = loadSession;
}
}
function saveData(){
let task = tasklist.innerHTML;
localStorage.setItem('savedData', task);
}
//to do list functionality
document.addEventListener('DOMContentLoaded', () => {
loadPreviousSessionTasks();
document.querySelector('form').onsubmit = () => {
const inputTask = document.querySelector('form')
if (inputTask.value === ''){
alert('write something')
return false;
}
else{
const task = document.querySelector('#task');
const li = document.createElement('li');
li.className = "list-group-item"
li.innerHTML = task.value;
const span = document.createElement('span');
span.innerHTML = '\u00d7';
//show on the Html document page
document.querySelector('#tasklist').append(li);
li.appendChild(span);
//save to local storage
saveData();
//clear input field
task.value = '';
//stop form from submitting
return false;
}
};
});
//removing of task and strike though when marked as checkd
tasklist.addEventListener('click', function(event){
if( event.target.tagName === "LI"){
event.target.classList.toggle("checked");
saveData();
}
else if(event.target.tagName === "SPAN"){
event.target.parentElement.remove();
saveData();
}
}, false)
///digital clock showing onthe page
function startTime() {
const today = new Date();
let h = today.getHours();
let m = today.getMinutes();
let s = today.getSeconds();
m = checkTime(m);
s = checkTime(s);
const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
document.getElementById('date').innerHTML = h + ":" + m + ":" + s;
setTimeout(startTime, 1000);
}
function checkTime(i) {
if (i < 10) {i = "0" + i}; // add zero in front of numbers < 10
return i;
};
document.addEventListener('DOMContentLoaded', startTime);
//tips showing code
function randomTips(){
const tips = {"Mahatma Gandhi":"Learn as if you will live forever, live like you will die tomorrow.",
"Eleanor Roosevelt": "When you give joy to other people, you get more joy in return. You should give a good thought to happiness that you can give out.",
"Diane McLaren": "It is only when we take chances, when our lives improve. The initial and the most difficult risk that we need to take is to become honest.",
"Herman Malville": "Success is not final; failure is not fatal: It is the courage to continue that counts.",
"Henry David Thoreau": "Success usually comes to those who are too busy looking for it.",
"Tony Robbins": "Setting goals is the first step in turning the invisible into the visible.",
"John Wooden": "Success is peace of mind, which is a direct result of self-satisfaction in knowing you made the effort to become the best of which you are capable",
"W. P. Kinsella": "Success is getting what you want, happiness is wanting what you get."
}
const header = Object.keys(tips)
const lengthHeader = header.length
//chosing a random index
const randomH = Math.ceil(Math.random()*lengthHeader) - 1
console.log(randomH);
//accesing that index content
const tipBody = tips[header[randomH]]
const tipHead = header[randomH]
//creating object for returning the tips
let tip = {};
tip[tipHead] = tipBody;
return tip
};
document.addEventListener('DOMContentLoaded', function(){
const cardTitle = document.querySelector('.card-title')
const cardText = document.querySelector('.card-text')
const tip = randomTips()
cardTitle.innerText = Object.keys(tip)[0]
cardText.innerText = tip[Object.keys(tip)[0]]
});
//weather showing on the page using weatherapi if this does not working that likely means you are seeing the code
//after 30th nov 2023. this is my end of the trial period date of that weather api if you want to revive this feature again
//go to search weatherapi.com on google sign in then they will give you an api key just replace after key= {your apikey}
//but till before
document.addEventListener('DOMContentLoaded', function(){
const savedLocation = localStorage.getItem('location');
fetch(`http://api.weatherapi.com/v1/current.json?key=53024ea6362d4014a7124444231611&q=${savedLocation}&aqi=no`)
.then(response => response.json())
.then( data => {
//temperature display as text
const temp = document.querySelector('#temp')
temp.innerText = data.current.temp_c
const tempIcon = document.querySelector('#weather-condition-icon')
tempIcon.src = 'https:'+ data.current.condition.icon
//location showing
const location = document.querySelector('#location')
location.innerText = data.location.name + ", " +data.location.region + ", " + `(${data.location.country})`
console.log(data)
console.log(data.current.temp_c)
console.log('https:'+ data.current.condition.icon)
});
});