diff --git a/.gitignore b/.gitignore index 6704566..999f9ee 100644 --- a/.gitignore +++ b/.gitignore @@ -102,3 +102,10 @@ dist # TernJS port file .tern-port + +# API Key +.apiKey + +#VS Code +.vscode + diff --git a/app.js b/app.js new file mode 100644 index 0000000..7c8f2b6 --- /dev/null +++ b/app.js @@ -0,0 +1,68 @@ +const locationDisplay = document.querySelector('#location'); +const weatherDisplay = document.querySelector('#weather'); +const forecastDisplay= document.querySelector('#sevenDay'); +// const userZip = prompt("Enter your Zipcode"); +const userZip = 19720; +// const userZip = document.querySelector('input').value; +const userLink = `http://api.zippopotam.us/us/${userZip}`; + +console.log(userZip) + + +function convertTemp(weatherLink) { + const celsius = weatherLink -273; + const fahrenheit = Math.floor(celsius * (9/5) + 32); + return fahrenheit; +} + +const client = new XMLHttpRequest(); + client.open("GET", userLink, true); + client.onreadystatechange = function() { + if(client.readyState == 4 && client.status === 200) { + + const zipData = JSON.parse(client.responseText); + locationDisplay.innerHTML = ` + ${zipData.places[0]['place name']}, ${zipData.places[0]['state abbreviation']} + `; + + userLon = `${zipData.places[0].longitude}`; + userLat = `${zipData.places[0].latitude}`; + + const weatherLink = `https://api.openweathermap.org/data/2.5/onecall?lat=${userLat}&lon=${userLon}&exclude=minutely,hourly&appid=${apikey}`; + + const sevenTimer = new XMLHttpRequest(); + sevenTimer.open("GET", weatherLink, true); + sevenTimer.onreadystatechange = () => { + if(sevenTimer.readyState == 4 && sevenTimer.status === 200) { + const weatherData = JSON.parse(sevenTimer.responseText); + const currentTemp = convertTemp(weatherData.current.temp); + weatherDisplay.innerHTML = `${currentTemp}\xB0F`; + } + } + sevenTimer.send(); + + const sevenDay = new XMLHttpRequest(); + sevenDay.open("GET", weatherLink, true); + sevenDay.onreadystatechange = () => { + if(sevenDay.readyState == 4 && sevenDay.status === 200) { + const weatherData = JSON.parse(sevenDay.responseText); + for(let i = 0; i <= 7; i++){ + const sevenDayMin = convertTemp(weatherData.daily[i].temp.min); + const sevenDayMax = convertTemp(weatherData.daily[i].temp.max); + const div = document.createElement('div'); + const divInnerHtml = + `