From 1403552a8107fcd832086ab6b8ecfe2e0cc3ea64 Mon Sep 17 00:00:00 2001 From: AustinKerr42 Date: Sat, 22 Aug 2020 18:36:05 -0500 Subject: [PATCH] Add option for fahrenheit --- js/weather.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/js/weather.js b/js/weather.js index 575d160..a55726d 100644 --- a/js/weather.js +++ b/js/weather.js @@ -8,7 +8,10 @@ weather.temperature = { unit: 'celsius', }; -const KELVIN = 273; +// Change to 'F' for Fahrenheit +var tempUnit = 'C'; + +const KELVIN = 273.15; // Use your own key for the Weather, Get it here: https://openweathermap.org/ const key = 'aa5b0a76dfbf87441928fb3cc32d3d68'; @@ -36,7 +39,8 @@ function getWeather(latitude, longitude) { return data; }) .then(function (data) { - weather.temperature.value = Math.floor(data.main.temp - KELVIN); + let celsius = Math.floor(data.main.temp - KELVIN); + weather.temperature.value = (tempUnit == 'C') ? celsius : (celsius * 9/5) + 32; weather.description = data.weather[0].description; weather.iconId = data.weather[0].icon; }) @@ -48,6 +52,6 @@ function getWeather(latitude, longitude) { // Display Weather info function displayWeather() { iconElement.innerHTML = ``; - tempElement.innerHTML = `${weather.temperature.value}°C`; + tempElement.innerHTML = `${weather.temperature.value}°${tempUnit}`; descElement.innerHTML = weather.description; }