-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.js
115 lines (109 loc) · 3.18 KB
/
weather.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
/*This javascript belongs to the Weather App
Description: This is a weather app that shows weather of pre-defined some cities*/
//Code to get city and state once entered
const cityWeather = [
{ location: "Ahmedabad, Gujarat", temperature: 39, weather: "Haze 🌫️" },
{
location: "Bangalore, Karnataka",
temperature: 28,
weather: "Cloudy ☁️️️☁️"
},
{ location: "Chennai, Tamil Nadu", temperature: 37, weather: "Cloudy ☁️☁️" },
{ location: "Hosur, Tamil Nadu", temperature: 28, weather: "Cloudy ☁️☁️" },
{
location: "Hyderabad, Telangana",
temperature: 28,
weather: "Thunderstorm ⛈️"
},
{
location: "Jaipur, Rajastan",
temperature: 32,
weather: "Mostly Cloudy ☁️"
},
{
location: "Kolkata, West Bengal",
temperature: 35,
weather: "Partly Cloudy ☁️"
},
{ location: "Mumbai, Maharashtra", temperature: 29, weather: "Cloudy ☁️☁️" },
{
location: "New Delhi, Delhi",
temperature: 39,
weather: "Mostly Cloudy ☁️"
},
{ location: "Pune, Maharashtra", temperature: 28, weather: "Cloudy ☁️☁️" },
{ location: "Surat, Gujarat", temperature: 38, weather: "Mostly Cloudy ☁️" },
{
location: "Tirunelveli, Tamil Nadu",
temperature: 34,
weather: "Mostly Cloudy️ ☁️"
}
];
function returnCity() {
var cityName = document.getElementById("city").value;
var cityFind = cityWeather.filter(function(something) {
if (something.location.includes(cityName)) return something;
});
var temp = cityFind[0].temperature;
var climate = cityFind[0].weather;
return (
(document.getElementById("cityState").innerHTML = cityName),
(document.getElementById("temperature").innerHTML = temp),
(document.getElementById("climate").innerHTML = climate)
);
}
function day() {
//Code for day
const DAYS = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
];
var today = new Date();
var dayIndex = today.getDay();
var day = DAYS[dayIndex];
//Code for time
var hours = today.getHours();
var minutes = today.getMinutes();
//code to convert 24hrs format to 12hrs format
if (hours === 0 || hours === 12) {
hours = 12;
} else if (hours < 12) {
hours = hours;
} else {
hours = hours - 12;
}
//code to include 0 in single digit numbers
if (minutes < 10) {
minutes = "0" + minutes;
} else {
minutes = minutes;
}
//code to display am and pm
if (today.getHours() < 12) {
var dayTag = "am";
} else {
var dayTag = "pm";
}
return (document.getElementById("dayTime").innerHTML =
day + ", " + hours + ":" + minutes + " " + dayTag);
setTimeout(updateClock, 1000);
}
function cal(val) {
var x;
var cityName = document.getElementById("city").value;
var cityFind = cityWeather.filter(function(something) {
if (something.location.includes(cityName)) return something;
});
var celcius = cityFind[0].temperature;
if (val === "C") {
return (document.getElementById("temperature").innerHTML = celcius);
} else if (val === "F") {
return (document.getElementById("temperature").innerHTML =
Math.round(((celcius * 9) / 5 + 32) * 10) / 10);
}
}