Converting weather in FCC weather app

Dear Fellows,

Could you please assist me with converting the weather part in my code it is working for the first time and then failing.

This is my codepen link

 var x = document.getElementById("weatherr").textContent;

    var tag = "celsius";

    if (tag === "celsius") {
      var x = x.replace(/[^0-9]+/, '');
      x = (x * 1.8) + 32;
      $("#weatherr").html("Weather at your town=" + x + " fahrenheit");
      tag = "fahrenheit";
    } else if (tag === "fahrenheit") {
      $("#weatherr").html(x);
      tag = "celsius";
    }
  1. You’re setting the variable tag to ‘celsius’ every time the slider is clicked, so only the first clause in the if block will ever run.
  2. var x = x.replace(/[^0-9]+/, '') is causing some problems for you. A better way to do this is to use a <span> tag for displaying your temperature. You could keep the rest of the message in the HTML, too.
<p id="weatherr">
    The temperature is <span id="temperature"></span>
</p>
// now we don't have to clear anything with regex
$('#temperature').text(newTemp)  
  1. Make sure you’re pulling the old temperature before overwriting it.
var oldTemp = $('#temperature').text();
var newTemp = // switch from f to c or c to f.
1 Like