Created
April 17, 2017 09:44
-
-
Save matchilling/b2fa9acf94e458f3d2866f5420c211c4 to your computer and use it in GitHub Desktop.
Convert wind direction in angles to cardinal direction created by matchilling - https://repl.it/HCSI/3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Convert wind direction in angles to cardinal direction | |
* @see https://en.wikipedia.org/wiki/Cardinal_direction | |
* | |
* @param {Number} degree | |
* @return {String} | |
*/ | |
function degreeToCardinalDirection(degree) { | |
const val = Math.floor(0.5 + (degree / 22.5)), | |
cardinalDirection = [ | |
'N', | |
'NNE', | |
'NE', | |
'ENE', | |
'E', | |
'ESE', | |
'SE', | |
'SSE', | |
'S', | |
'SSW', | |
'SW', | |
'WSW', | |
'W', | |
'WNW', | |
'NW', | |
'NNW' | |
]; | |
return cardinalDirection[(val % 16)]; | |
} | |
for (var i = 0; i <= 360; i += 22.5) { | |
console.log(`${i / 22.5 + 1}. ${i} degree converts to "${degreeToCardinalDirection(i)}".`) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment