83502_assigned/Develop/app.js
let userInput;
let uvIndex;
let pastCities = JSON.parse(localStorage.getItem("cities")) || [];
let display = document.getElementById("cityInfo");
let fiveSelect = document.getElementById("fiveDay");
let citySelect = document.getElementById("pastCities");
API KEY : 4e5dbe7db2b5e9c8b47fa40b691443d5
API call by city name: api.openweathermap.org/data/2.5/weather?q=
document.addEventListener("click", () => {
let target = event.target;
if (target.classList.contains("btn")) {
userInput = document.getElementById("citySearch").value;
console.log(userInput)
displayWeather(userInput);
empty out userInput
document.getElementById("citySearch").value = "";
}
});
const renderPastCity = (_) => {
set to empty before every rende
citySelect.innerHTML = "";
for (let i = 0; i < pastCities.length; i++) {
let cityNode = document.createElement("div");
cityNode.innerHTML = `${pastCities[i]}
`;
citySelect.append(cityNode);
}
};
const toFarenheit = (value) => {
value = (value * (9 / 5) - 459.67).toFixed(2);
return value;
};
Capitalize first letter of every word in the string
const titleCase = (str) => {
let splitStr = str.toLowerCase().split(" ");
console.log(splitStr)
for (let i = 0; i < splitStr.length; i++) {
splitStr[i] =
splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
}
return splitStr.join(" ");
};
const displayWeather = (userInput) => {
pushing userInput to local storage
userInput = titleCase(userInput);
pastCities.push(userInput);
console.log(pastCities);
localStorage.setItem("cities", JSON.stringify(pastCities));
renderPastCity();
getCityWeather(userInput);
};
const getCityWeather = (userInput) => {
display.innerHTML = "";
fetch(
`https:
api.openweathermap.org/data/2.5/weather?q=${userInput}&appid=4e5dbe7db2b5e9c8b47fa40b691443d5`
)
.then((response) => response.json())
.then(
({ main: { temp, humidity }, wind: { speed }, coord: { lon, lat } }) => {
let info = document.createElement("div");
temp = toFarenheit(temp);
console.log(temp, humidity, speed, lon, lat)
info.innerHTML = `
${userInput} ${moment().format("MM/DD/YYYY")}
h2
Temperature: ${temp} ºF
p
Humidity: ${humidity}
p
Wind Speed: ${speed} mph
p
`;
display.append(info);
getUvIndex(lon, lat);
getFiveDayForecast(lon, lat);
}
)
.catch((e
or) => console.e
or(e
or));
};
const getUvIndex = (lon, lat) => {
fetch(
`https:
api.openweathermap.org/data/2.5/uvi?appid=4e5dbe7db2b5e9c8b47fa40b691443d5&lat=${lat}&lon=${lon}`
)
.then((response) => response.json())
.then(({ value }) => {
...