I am trying to create a reverse heading app and have gotten the subtraction part of it to work but when I punch in 30 for example instead of pumping out 210 degrees it pumps out 30180. My code is below and I would love some suggestions on how to fix this.
if(convertFromValue > 360){
paraGraph.innerHTML = 'Please enter a number between 1 and 360';
}
else
{
if(convertFromValue > 180) {
paraGraph.innerHTML = convertFromValue - 180 ;
}
else
{
paraGraph.innerHTML = convertFromValue + 180 ;
}
}
}
JavaScript gives preference to strings around the
+operator. You have to explicitly make sure that the values are really numbers:That may or may not be the right thing for your application. There are other common shortcuts to converting strings to numbers. Of course you probably want to check for errors too, because if the string is coming from an input field it might not be (or look like) a number at all.