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
107 changes: 76 additions & 31 deletions coding-exercises/app.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,77 @@
window.addEventListener('load', () => {
/**
* TODO : Create your 5 variables to get elements
* based on the classes you created in the `index.html`.
*
* Define your variables below this comment.
*/

if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(position => {
console.log('My General Position:', position);
const long = position.coords.longitude;
const lat = position.coords.latitude;

/* TODO: Continue your fetch request to set the DOM Elements for
* temperature, description/summary, and timezone.
* Make sure to include error handling.
*/

/*TODO: Set the weather icon */

/**TODO: Add an event listener that toggles between Fahrenheit and Celcius
* when a user clicks on the current temperature section.
*/
});
}

/**
* TODO: Code out the remainder of `setIcons`function.
*/
const setIcons = () => {};
window.addEventListener("load", () => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
position => {
const lat = position.coords.latitude;
const long = position.coords.longitude;
getWeather(lat, long);
},
function(e) {
window.alert("This app needs your location to function properly!");
}
);
}
});

const setIcons = (icon, iconID) => {
const skycons = new Skycons({ color: "white" });
skycons.set(icon, iconID);
skycons.play();
};

async function getWeather(lat, long) {
const fixedLat = Number(lat.toFixed(4));
const fixedLong = Number(long.toFixed(4));

const corsAnywhereUrl = "https://cors-anywhere.herokuapp.com/";
const apiUrl = `https://api.darksky.net/forecast/e0391512f84780b1d9fb3bb1f8f708cd/${fixedLat},${fixedLong}`;
let weatherReq;
try {
weatherReq = await fetch(corsAnywhereUrl + apiUrl);
} catch (e) {
paintDom(null, false);
console.error(e);
}
const json = await weatherReq.json();
paintDom(json, true);
}

function paintDom(json, sucessful) {
if (!sucessful) {
title.innerText = "Error loading... Please try again later";
tempValue.innerText = tempUnit.innerText = tempDesc.innerText = "...";
return;
}

const title = document.getElementsByClassName("title")[0];
const iconCanvas = document.getElementsByClassName("animated-icon")[0];
const tempValue = document.getElementsByClassName("temp-value")[0];
const tempUnit = document.getElementsByClassName("temp-unit")[0];
const tempDesc = document.getElementsByClassName("temp-desc")[0];

title.innerText = `Timezone: ${json.timezone
.split("/")
.reverse()
.join(", ")
.replace(/_/gm, " ")}`;

tempValue.innerText = `Temperature: ${json.currently.temperature}`;
tempUnit.innerText = `Fahreinheit, ${json.flags.units.toUpperCase()}`;
tempDesc.innerHTML = `Currently <strong>${json.currently.summary}</strong>`;
setIcons(iconCanvas, json.currently.icon);

document
.getElementsByClassName("degree-section")[0]
.addEventListener("click", function() {
if (tempUnit.innerText.includes("US")) {
tempValue.innerText = `Temperature: ${(
json.currently.temperature -
32 * (5 / 9)
).toFixed(2)}`;
tempUnit.innerText = "Celsius, EARTH";
} else {
tempValue.innerText = `Temperature: ${json.currently.temperature}`;
tempUnit.innerText = `Fahreinheit, ${json.flags.units.toUpperCase()}`;
}
});
}
21 changes: 10 additions & 11 deletions coding-exercises/index.html
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="./style.css" />
<link rel="icon" href="#" />
<title>Weather App</title>
</head>

<body>
<div class="location">
<!-- TODO: give this h1 a class-->
<h1>PLACEHOLDER: New York</h1>
<!-- TODO: give this canvas element a class. This element will hold an animated icon-->
<canvas />
<h1 class="title">PLACEHOLDER: New York</h1>
<canvas class="animated-icon" width="128" height="128"></canvas>
</div>
<div class="temperature">
<div class="degree-section">
<!-- TODO: give this h2 a class-->
<h2>PLACEHOLDER: 32</h2>
<!-- TODO: give this span a class-->
<span>PLACHOLDER: F</span>
<h2 class="temp-value">PLACEHOLDER: 32</h2>
<span class="temp-unit">PLACHOLDER: F</span>
</div>
<!-- TODO: give this div a class-->
<div>PLACEHOLDER Description: Snowing</div>
<div class="temp-desc">PLACEHOLDER Description: Snowing</div>
</div>

<!-- TODO: During the appropriate step in this problem set, place the skycon script here-->
<script type="text/javascript" src="skycons.js"></script>
<script src="app.js"></script>
</body>

</html>
Loading