I'm building an app that displays the weather and converts the current temp from Fahrenheit to Celsius. For some reason, when the conversion takes place, the math isn't coming out right. Oddly, after clicking the button 5 - 6 times, it starts working correctly.
My best guess is that I'm doing something wrong with parseInt()
or maybe a variable isn't being updated.
Thoughts?
function toggleUnits(){
if(wUnit.innerHTML == "C"){
var oldTemp = parseInt(wTemp.innerHTML, 10),
newTemp = oldTemp * 9 / 5 + 32;
wTemp.innerHTML= newTemp;
wUnit.innerHTML = "F";
unitToggle.innerHTML ="Switch to Celsius";
}else{
var oldTemp = parseInt(wTemp.innerHTML, 10),
newTemp = (oldTemp-32) * 5 / 9;
wTemp.innerHTML= newTemp;
wUnit.innerHTML = "C";
unitToggle.innerHTML ="Switch to Fahrenheit";
}
}