Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
17 changes: 15 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,22 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<title>Weather App</title>
<link href="weatherapp.css" rel="stylesheet">
</head>
<body>
<h1>Hola Mundo</h1>
<div>
<h1>Weather App</h1>

<input type="number" id="zipcode" placeholder="Enter zipcode">
<button id="forecast">Enter zipcode</button>
<div id="weatherinfo"></div>
<p id="output"></p>
<p id="location"></p>
</div>



<script src="weatherapp.js"></script>
</body>
</html>
7 changes: 7 additions & 0 deletions weatherapp.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
body {
background-color: white;
font-family: Arial, Helvetica, sans-serif;
font-size: 1.1em;
color: gray;
}

54 changes: 54 additions & 0 deletions weatherapp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

const apiKey = "9d60ac7e8eb3d17560a9e4046dc3f540";
// add click handler for button
document.getElementById("forecast").addEventListener("click", getData);

function getData() {
// console.log("get data");
// const weatherInfo = document.getElementById("weatherInfo");
// need the zipcode entered
let zipcode =
document.getElementById("zipcode").value;
// console.log("zipcode is " + zipcode);
// set up api
const endpoint =
"https://api.openweathermap.org/data/2.5/forecast"
// "https://api.openweathermap.org/data/2.5/weather";
// "http://api.zippopotam.us/US/"+zipcode;
const query = "?zip=" + zipcode + "&units=imperial&appid=" + apiKey;
const url = endpoint + query;
// console.log(url);
const xhr = new XMLHttpRequest();
// set up response
xhr.addEventListener("load", responseReceivedHandler);
// required for json
xhr.responseType = "json";
// open the connection "endpoint"
xhr.open("GET", url);
xhr.send();

};

function responseReceivedHandler() {
const weatherInfo = document.getElementById("output");
if (this.status === 200) {
console.log(this.response);
const data = this.response;

// put data on the page
let output = "<p>City: " + data.city.name + " </p>";
// output += "<p>Current temp: " + data.main.temp + "&deg;F</p>";
output += "<p>Current temp: " + data.list[0].main.temp + "&deg;F</p>";
output += "<p>Latitude: " + data.city.coord.lat + "</p>";
output += "<p>Longitude: " + data.city.coord.lon + "</p>";
// output += "<p>Description: " + data.weather[0].description + "</p>";
// output += "<p>Humidity: " + data.main.humidity + "%</p>";
// display this in the div

weatherInfo.innerHTML = output;
}
else {
weatherInfo.innerHTML = "City Unavailable";
}
}