Function that converts degrees to cardinal and intercardinal directions
✓ Published0🌍 Public
Aajzeigert
Last edited Mar 3, 2016
Created on Mar 3, 2016
This example shows a JavaScript function that converts numeric degree values into cardinal and intercardinal compass directions, such as N, NE, E, and SE. It displays the logic by defining `getDirection`, which takes a degree input and returns the corresponding direction based on a series of conditional ranges. The function uses plain JavaScript comparison operators, with ranges centered on 22.5-degree increments, and wraps values at 337.5 degrees back to “N”. The code is designed for use with geographic data, such as aspect values in mapping contexts, though it does not rely on any external libraries or rendering APIs.
AI-generated description