Good catch @guidoleijten ... Yep... a closer look at the function shows greater than `num.dec` and less than `num.dec` which leaves all exact `num.dec` to fall through as N

This explains the occasional glitch I have been seeing... one I had attributed to browser refresh or something interrupting the 10 minute update cycle I use.

A simple tweak changing the rest of the `<` symbols to `<=` corrected that.

As was in the initial beginning of the function

"New and Improved... Now with 99% more = "

``````let degreesToCardinal = function(deg){
if (deg>11.25 && deg<=33.75){return "NNE";}
else if (deg>33.75 && deg<=56.25){return "NE";}
else if (deg>56.25 && deg<=78.75){return "ENE";}
else if (deg>78.75 && deg<=101.25){return "E";}
else if (deg>101.25 && deg<=123.75){return "ESE";}
else if (deg>123.75 && deg<=146.25){return "SE";}
else if (deg>146.25 && deg<=168.75){return "SSE";}
else if (deg>168.75 && deg<=191.25){return "S";}
else if (deg>191.25 && deg<=213.75){return "SSW";}
else if (deg>213.75 && deg<=236.25){return "SW";}
else if (deg>236.25 && deg<=258.75){return "WSW";}
else if (deg>258.75 && deg<=281.25){return "W";}
else if (deg>281.25 && deg<=303.75){return "WNW";}
else if (deg>303.75 && deg<=326.25){return "NW";}
else if (deg>326.25 && deg<=348.75){return "NNW";}
else {return "N";}
}
``````

With a bit Math may be simpler

``````function degToCompass(num) {
var val = Math.floor((num / 22.5) + 0.5);
var arr = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"];
return arr[(val % 16)];
}
``````
1 Like

Great minds think alike..

Yes, unfortunately Math and I do not compute So I tend to just grab what I can find and shoehorn it into my code

Eventually I tweak my stuff as I get a better understanding of the improvements offered. Thanks

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.