forked from FSOC-OSS/Fsoc-level-medium
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
116 lines (90 loc) · 3.68 KB
/
Copy pathscript.js
File metadata and controls
116 lines (90 loc) · 3.68 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
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
document.addEventListener('DOMContentLoaded', () => {
// --- Block A: Element Hooks ---
const taskInput = document.getElementById('task-input');
const addTaskBtn = document.getElementById('add-task-btn');
const taskList = document.getElementById('task-list');
const clearAllBtn = document.getElementById('clear-all-btn');
const cityInput = document.getElementById('city-input');
const searchWeatherBtn = document.getElementById('search-weather-btn');
const weatherInfo = document.getElementById('weather-info');
const themeToggle = document.getElementById('theme-toggle');
const copyrightYear = document.querySelector('footer p');
// --- Block B: Data Store ---
let tasks = [];
// --- Block C: Service Configuration ---
const weatherApiKey = 'YOUR_API_KEY_HERE';
// --- Block D: Module 1 Functions ---
function renderTasks() {
taskList.innerHTML = '';
tasks.forEach((task, index) => {
const li = document.createElement('li');
li.className = 'task-item';
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.checked = task.completed;
checkbox.addEventListener('change', () => toggleTaskCompletion(index));
const taskText = document.createElement('span');
taskText.textContent = task.text;
const deleteBtn = document.createElement('button');
deleteBtn.className = 'delete-btn';
deleteBtn.textContent = '🗑️';
li.appendChild(checkbox);
li.appendChild(taskText);
li.appendChild(deleteBtn);
taskList.appendChild(li);
});
}
function addTask() {
const text = taskInput.value.trim();
if (text) {
tasks.push({ text: text, completed: false });
renderTasks();
}
}
function clearAllTasks(){
tasks=[]
}
// --- Block E: Module 2 Functions sample data ---
async function fetchWeather(city) {
const url = `write something here `;
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Request failed (${response.status})`);
}
const data = await response.json();
displayWeather(data);
} catch (error) {
console.error('Service call failed:', error);
weatherInfo.innerHTML = `<p class="error-text">Data unavailable.</p>`;
}
}
function displayWeather(data) {
const { name, main, weather } = data;
const iconUrl = `http://openweathermap.org/img/wn/${weather[0].icon}@2x.png`;
weatherInfo.innerHTML = `
<h3>${name}</h3>
<img src="${iconUrl}" alt="${weather[0].description}" class="weather-icon">
<p>Temperature: ${main.temp}°C</p>
<p>Condition: ${weather[0].main}</p>
`;
}
// --- Block F: Event Registry ---
addTaskBtn.addEventListener('click', addTask);
clearAllBtn.addEventListener('click', clearAllTasks);
searchWeatherBtn.addEventListener('click', () => {
const city = cityInput.value.trim();
if (city) {
fetchWeather(city);
}
});
themeToggle.addEventListener('click', () => {
document.body.classList.toggle('dark-theme')
});
// --- Block G: Application Entry Point ---
function init() {
fetchWeather("sdfasdfnsa,mn,mn.");
renderTasks();
}
init();
});