Hi All,
I’ve got my weather app working, and I’m pretty proud of that, but I’m trying to convert the temperature from F to C and back (per this user story: I can push a button to toggle between Fahrenheit and Celsius.)
With my code, I can switch it to C, but it doesn’t switch back. I’ve combed through the forum, googled everything, and I don’t know what is wrong. Can you help?
Here’s the relevant part ot the JS
//change F and C
$(".btn").on("click", function() {
var units = response.flags.units;
if (units === "us") {
$currentTemp.html("");
var celsius = Math.round((farenheit - 32)/1.8);
$currentTemp.append(celsius + " C");
units = "si";
console.log(units);
} else if (units === "si") {
$currentTemp.html("");
$currentTemp.append(farenheit + " F");
units = "us";
}//close else
});//close button click
If it helps, here’s all of it:
$(document).ready(function() {
// declare variables
var url = "https://api.darksky.net/forecast/e7522759282f3ef892f854a38a7ef757/";
var latitude = null;
var longitude = null;
$currentTemp = $("#currentTemp");
$currentCond = $("#currentCond");
$currentWind = $("#currentWind");
$forecast = $("#forecast");
// use geolocation to get the current position
function getLocation() {
var geolocation = navigator.geolocation;
geolocation.getCurrentPosition(showLocation);
}
function showLocation(position) {
latitude = position.coords.latitude;
longitude = position.coords.longitude;
darkSky(latitude, longitude);
}
getLocation();
// make the Dark Sky function
function darkSky(latitude, longitude) {
url = url + latitude + "," + longitude + "?exclude=minutely,hourly,alerts";
// make the ajax call to Dark Sky for weather info
$.ajax({
type: 'GET',
url: url,
dataType: 'jsonp',
success: function(response) {
// get current conditions
var farenheit = Math.round(response.currently.temperature);
$currentTemp.append(Math.round(response.currently.temperature) + " F");
$currentCond.append(response.currently.summary);
$currentWind.append(response.currently.windSpeed + " mph");
// get weekly forecast
var result = response.daily.data;
for(var i = 0; i < 5; i++) {
$forecast.append("<div class='box'>" + result[i].summary + "<br>" + Math.floor(result[i].temperatureMin) + "<br>" + Math.floor(result[i].temperatureMax) + "</div>");
}
/*
// toggle F and C
$(".btn").click(function(){
$currentCond.toggleClass("celsius");
setCelsius();
});
function setCelsius(){
if(".celsius" === true) {
$currentTemp.html("");
var celsius = Math.round((farenheit - 32)/1.8);
$currentTemp.append(celsius + " C");
}
}
*/
//change F and C
$(".btn").on("click", function() {
var units = response.flags.units;
if (units === "us") {
$currentTemp.html("");
var celsius = Math.round((farenheit - 32)/1.8);
$currentTemp.append(celsius + " C");
units = "si";
console.log(units);
} else if (units === "si") {
$currentTemp.html("");
$currentTemp.append(farenheit + " F");
units = "us";
}//close else
});//close button click
}//close success
});//close ajax
}//close darkSky
});//close document.ready
And I’ll also link to my codepen: https://codepen.io/emorgan05/pen/NgEYNx
Thanks!