Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save matchilling/b2fa9acf94e458f3d2866f5420c211c4 to your computer and use it in GitHub Desktop.
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
/**
* 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