Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
61 lines (48 loc) · 2.31 KB
/
Copy pathscript.js
File metadata and controls
61 lines (48 loc) · 2.31 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
const search = document.querySelector(".search");
function capitalizeEachWord(text) {
return text
.split(' ') // Split text into an array of words
.map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()) // Capitalize each word
.join(' '); // Join the words back into a string
}
search.addEventListener('keydown', function(event) {
if (event.key === 'Enter') {
event.preventDefault();
const text = search.value;
const location = capitalizeEachWord(text);
console.log(location);
const temp = document.querySelector(".temp")
const city_name = document.querySelector(".name")
const Time= document.querySelector(".time")
const Date = document.querySelector(".date")
const cloud = document.querySelector(".Cloud")
const Humidity = document.querySelector(".Humidity")
const Wind = document.querySelector(".Wind")
const Condition = document.querySelector(".condition")
const url = `https://api.weatherstack.com/current?access_key=a5f6b61790a6fd119c4de2f5f838d0de&query=${location}`;
const options = {
method: 'GET'
};
async function fetchWeatherData() {
try {
const response = await fetch(url, options);
console.log(response);
const data = await response.json();
console.log(data);
const { name, country, localtime } = data.location;
const {cloudcover,temperature,weather_descriptions,weather_icons,wind_speed,humidity } = data.current;
console.log(cloudcover,name,country,temperature,weather_descriptions,weather_icons,wind_speed);
temp.innerText = `${temperature} °C`
city_name.innerText = name
Date.innerText = localtime
Wind.innerText = `${wind_speed} Kms/hr`;
Humidity.innerText = `${humidity} %`
Condition.innerText = weather_descriptions
cloud.innerText = `${cloudcover} %`
} catch (error) {
console.error(error);
}
}
fetchWeatherData();
}
});