-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
115 lines (91 loc) · 3.76 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
$(document).ready(function () {
var cities = JSON.parse(localStorage.getItem('cities')) || [];
function renderCities() {
$("#previousSearchList").empty();
for (var i = 0; i < cities.length; i++) {
$("#previousSearchList").append('<button type="button" class="btn btn-primary btn-md btn-block oldCity">' + cities[i] + '</button>')
}
}
function getWeather(city) {
var queryURL =
"https://api.openweathermap.org/data/2.5/weather?q=" +
city +
"&appid=1342e1bc1df48134a2a819f9c3969c81";
$.ajax({
url: queryURL,
method: "GET",
}).then(function (response) {
var d = new Date();
var todaysDate =
(d.getMonth() + 1) + "/" + d.getDate() + "/" + d.getFullYear();
var tempF = Math.floor((response.main.temp - 273.15) * 1.8 + 32);
var cityImage = "http://openweathermap.org/img/wn/" + response.weather[0].icon + "@2x.png";
$("#cityNameText").html(response.name + " " + todaysDate);
$('.cityImage').append('<img src="' + cityImage + '"/>');
$(".currentHumidity").html("Current Humidity: " + response.main.humidity + "%");
$(".currentTemp").html("Current Temperature: " + tempF + "\xB0 F");
$(".currentWind").html(
"Current Wind Speed: " + response.wind.speed + "MPH"
);
var longitude = response.coord.lon;
var latitude = response.coord.lat;
queryURLBig = "https://api.openweathermap.org/data/2.5/onecall?lat=" + latitude + "&lon=" + longitude + "&appid=1342e1bc1df48134a2a819f9c3969c81";
$.ajax({
url: queryURLBig,
method: "GET",
}).then(function (response) {
$("#uvColor").text(response.current.uvi)
var UVNumber = parseFloat(response.current.uvi)
if (UVNumber < 3) {
$("#uvColor").addClass("favorable")
$("#uvColor").removeClass("moderate severe")
}
else if (UVNumber >= 3 && UVNumber < 8) {
$("#uvColor").addClass("moderate")
$("#uvColor").removeClass("favorable severe")
}
else {
$("#uvColor").addClass("severe")
$("#uvColor").removeClass("favorable moderate")
}
//creating the for loop to iterate through the days - set up one and make sure it works, and THEN go back and replace it w "i"
for (var i = 1; i < 6; i++) {
var fiveDayTemp = Math.floor((response.daily[i].temp.day - 273.15) * 1.8 + 32);
var ts = response.daily[i].dt
var ts_ms = ts * 1000;
var date_ob = new Date(ts_ms);
var year = date_ob.getFullYear();
var month = ("0" + (date_ob.getMonth() + 1)).slice(-2);
var date = ("0" + date_ob.getDate()).slice(-2);
var fiveDayDate = (month + "/" + date + "/" + year)
var weatherIconForecast = "http://openweathermap.org/img/wn/" + response.daily[i].weather[0].icon + "@2x.png"
//template literals or string interpolation-- dynamically selects
$(`.dateText${i}`).html(fiveDayDate);
$(`#weatherIcon${i}`).attr("src", weatherIconForecast);
$(`.tempText${i}`).html("Temp: " + fiveDayTemp + "\xB0 F");
$(`.humidityText${i}`).html("Humidity: " + response.daily[i].humidity + "%");
}
});
});
};
$("#searchBtn").on("click", function (event) {
event.preventDefault();
function saveToStore() {
localStorage.setItem("cities", JSON.stringify(cities));
}
var city = $("#searchedCity").val();
if (!cities.includes(city)) {
cities.push(city);
console.log(cities);
saveToStore();
}
renderCities();
getWeather(city);
});
renderCities()
$("#previousSearchList").on("click", ".oldCity", function (event) {
event.preventDefault();
var city = $(this).text();
getWeather(city);
});
});