Local Weather App- not able to toggle the temperature successfully

Local Weather App- not able to toggle the temperature successfully
0

#1

I’ve been trying this for few days and also been looked at some similar issues from other campers but still can’t figure it out.

Would be grateful if someone can take a look at my code. Thanks in advance!


#2

Add to css

h2 {
  user-select: none;
}

Replace javascript

      var ftemp = result.currently.temperature;
      var ctemp = (ftemp - 32) / 1.8;
      var isFahrenheit = true;
      //change temp
      $("h2").on("click", function(temp) {
        if (isFahrenheit) {
          $("h2").html(Math.floor(ctemp) + `<a href=#>&deg;C</a>`);
        } else {
          $("h2").html(Math.floor(ftemp) + `<a href=#>&deg;F</a>`);
        }
        isFahrenheit = !isFahrenheit;
      }); //onclick

The issue was with an ‘a’ html tag not being subscribed to the event because within your function you are replacing it.


#3

Thank you for your help! It works now~


#4

Btw, would you please explain to me that how come it won’t work if I put
isFahrenheit = !isFahrenheit; within the else bracket ?


#5

isFahrenheit = !isFahrenheit; is basically toggling value from true to false or false to true.
If you put isFahrenheit = !isFahrenheit; within else statement then you will end up in a sort of ‘loop’ where the value of isFahrenheit will never become true again.


#6

I see. Thank you very much!!