Question

I'm write this bit of code to convert azimuth degrees to human redable, but the conversion is not more accurate, in cases of degrees is slightly smaller than 360.

function azimutHuman(ang) {     //can be 0 - 360

    var azimuthHumans = [
              'Nord','Nord-NE','Nord-Est','Est-NE',
              'Est','Est-SE','Sud-Est','Sud-SE',
              'Sud','Sud-SO','Sud-Ovest','Ovest-SO',
              'Ovest','Ovest-SO','Nord-Ovest','Nord-NO'];

    return azimuthHumans[ Math.round(ang/22.5) ];
}

for example:

azimutHuman(350);   //return undefined instead of "Nord"
Was it helpful?

Solution

350 -> 360 = 0->10 : 360 is north as will as 0 .

so you need to add an element "Nord" again .try this function now :

function azimutHuman(ang) {     //can be 0 - 360

    var azimuthHumans = [
              'Nord','Nord-NE','Nord-Est','Est-NE',
              'Est','Est-SE','Sud-Est','Sud-SE',
              'Sud','Sud-SO','Sud-Ovest','Ovest-SO',
              'Ovest','Ovest-SO','Nord-Ovest','Nord-NO','Nord'];

    return azimuthHumans[ Math.round(ang/22.5) ];
}

OTHER TIPS

Math.round(350/22.5) = 16 Your array at index 16 is undefined. Add a 16th element to it, Nord again should work

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top