const CURRENT_LOCATION = document.getElementsByClassName('weather-content__overview')[0];
const CURRENT_TEMP = document.getElementsByClassName('weather-content__temp')[0];
const FORECAST = document.getElementsByClassName('component__forecast-box')[0];
// Use Fetch API to GET data from OpenWeather API
// return json
function getWeatherData(position) {
let headers = new Headers();
const URL = `https://api.openweathermap.org/data/2.5/forecast/daily?${position}&cnt=7&units=imperial&APPID=e43f64ee98be9268f7a7f49e34aecfdf`;
return fetch(URL, {
method: 'GET',
headers: headers
}).then(data => data.json());
}
// TUTORIAL READERS:
// yeah, using an external resource for the icons and applying them here using a switch block
function applyIcon(icon) {
let selectedIcon;
switch (icon) {
case '01d':
selectedIcon = "wi-day-sunny"
break;
case '01n':
selectedIcon = "wi-night-clear"
break;
case '02d':
case '02n':
selectedIcon = "wi-cloudy"
break;
case '03d':
case '03n':
case '04d':
case '04n':
selectedIcon = "wi-night-cloudy"
break;
case '09d':
case '09n':
selectedIcon = "wi-showers"
break;
case '10d':
case '10n':
selectedIcon = "wi-rain"
break;
case '11d':
case '11n':
selectedIcon = "wi-thunderstorm"
break;
case '13d':
case '13n':
selectedIcon = "wi-snow"
break;
case '50d':
case '50n':
selectedIcon = "wi-fog"
break;
default:
selectedIcon = "wi-meteor"
}
return selectedIcon;
}
// Use returned json from promise to render daily forecast
renderData = (location, forecast) => {
// render city, current weather description and temp
const currentWeather = forecast[0].weather[0];
const widgetHeader = `<h1>${location.name}</h1><small>${currentWeather.description}</small>`;
CURRENT_TEMP.innerHTML = `<i class="wi ${applyIcon(currentWeather.icon)}"></i>...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.