Skip to content

Commit a46b162

Browse files
committed
added opposite unit processing to weather page (cause 341 people used it this week apparently its a thing)
1 parent 85b92a1 commit a46b162

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

static/assets/scripts/weather.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ function updateUI(data) {
8282
const currentContainer = document.getElementById('current');
8383
currentContainer.innerHTML = `
8484
<h2>Current Weather for ${data.city || "Unknown"}</h2>
85-
<div class="temperature">${Math.round(data.current.temperature_2m)}${data.current_units.temperature_2m}</div>
85+
<div class="temperature">${Math.round(data.current.temperature_2m)}${data.current_units.temperature_2m} (${otherUnit(data.current.temperature_2m, data.current_units.temperature_2m)})</div>
8686
<div class="weather-icon">${weatherIcons[data.current.weather_code] || '❓'}</div>
8787
<div class="conditions">
8888
Feels like: ${Math.round(data.current.apparent_temperature)}${data.current_units.apparent_temperature}<br>
@@ -98,8 +98,8 @@ function updateUI(data) {
9898
<div class="date">${formatDate(date)}</div>
9999
<div class="weather-icon">${weatherIcons[data.daily.weather_code[index]] || '❓'}</div>
100100
<div class="conditions">
101-
High: ${Math.round(data.daily.temperature_2m_max[index])}${data.daily_units.temperature_2m_max}<br>
102-
Low: ${Math.round(data.daily.temperature_2m_min[index])}${data.daily_units.temperature_2m_min}<br>
101+
High: ${Math.round(data.daily.temperature_2m_max[index])}${data.daily_units.temperature_2m_max} (${otherUnit(data.daily.temperature_2m_max[index], data.daily_units.temperature_2m_max)})<br>
102+
Low: ${Math.round(data.daily.temperature_2m_min[index])}${data.daily_units.temperature_2m_min} (${otherUnit(data.daily.temperature_2m_min[index], data.daily_units.temperature_2m)})<br>
103103
Rain: ${data.daily.precipitation_sum[index]}${data.daily_units.precipitation_sum}
104104
</div>
105105
</div>
@@ -133,4 +133,18 @@ async function initWeather() {
133133
}
134134

135135
// Start the application
136-
initWeather();
136+
initWeather();
137+
function otherUnit(value, unit){
138+
if (unit === "°C") {
139+
return Math.round(convertToFahrenheit(value)) + "°F";
140+
} else if (unit === "°F") {
141+
return Math.round(convertToCelsius(value)) + "°C";
142+
}
143+
return value;
144+
}
145+
function convertToFahrenheit(celsius) {
146+
return (celsius * 9/5) + 32;
147+
}
148+
function convertToCelsius(fahrenheit) {
149+
return (fahrenheit - 32) * 5/9;
150+
}

0 commit comments

Comments
 (0)