Hello everyone,
I’m struggling with my Weather Machine project. It’s almost done, but I can’t create working button to switch between metric and imperial units. I think it’s something about functions scope, because everything works nice separately, but doesn’t want to run together.
Here’s my Pen: https://codepen.io/mmicalt/pen/WZWyOx?editors=0011
each function is described, I believe that issue is between getTemp() function and ‘#switch’ button event handler.
I know the solution will probably be really simple, but I’m still missing it
you were declaring var with strings instead of referencing another variable.
also, there was a problem with var scope.
// calculate adn display right temperature
var celsius;
var fahrenheit;
var degrees;
function getTemp(temp){
celsius = Math.floor(temp) + "° C";
fahrenheit = Math.floor( (temp) * 9/5 + 32 ) + "° F";
degrees = celsius
$("#temperature").text(degrees);
}
// switch between Celsius and Fahrenheit
$("#switch").click(function(){
(degrees === celsius) ? degrees = fahrenheit : degrees = celsius;
$("#temperature").text(degrees);
});
1 Like
Thank you, now it works fine that was so stupid mistake of me