diff --git a/mgrs.js b/mgrs.js index 9db968c..694525f 100644 --- a/mgrs.js +++ b/mgrs.js @@ -297,72 +297,22 @@ function UTMtoLL(utm) { * for. * @return {char} The letter designator. */ -function getLetterDesignator(lat) { - //This is here as an error flag to show that the Latitude is - //outside MGRS limits - let LetterDesignator = 'Z'; - - if ((84 >= lat) && (lat >= 72)) { - LetterDesignator = 'X'; - } - else if ((72 > lat) && (lat >= 64)) { - LetterDesignator = 'W'; - } - else if ((64 > lat) && (lat >= 56)) { - LetterDesignator = 'V'; - } - else if ((56 > lat) && (lat >= 48)) { - LetterDesignator = 'U'; - } - else if ((48 > lat) && (lat >= 40)) { - LetterDesignator = 'T'; - } - else if ((40 > lat) && (lat >= 32)) { - LetterDesignator = 'S'; - } - else if ((32 > lat) && (lat >= 24)) { - LetterDesignator = 'R'; - } - else if ((24 > lat) && (lat >= 16)) { - LetterDesignator = 'Q'; - } - else if ((16 > lat) && (lat >= 8)) { - LetterDesignator = 'P'; - } - else if ((8 > lat) && (lat >= 0)) { - LetterDesignator = 'N'; - } - else if ((0 > lat) && (lat >= -8)) { - LetterDesignator = 'M'; - } - else if ((-8 > lat) && (lat >= -16)) { - LetterDesignator = 'L'; - } - else if ((-16 > lat) && (lat >= -24)) { - LetterDesignator = 'K'; - } - else if ((-24 > lat) && (lat >= -32)) { - LetterDesignator = 'J'; - } - else if ((-32 > lat) && (lat >= -40)) { - LetterDesignator = 'H'; - } - else if ((-40 > lat) && (lat >= -48)) { - LetterDesignator = 'G'; - } - else if ((-48 > lat) && (lat >= -56)) { - LetterDesignator = 'F'; - } - else if ((-56 > lat) && (lat >= -64)) { - LetterDesignator = 'E'; - } - else if ((-64 > lat) && (lat >= -72)) { - LetterDesignator = 'D'; - } - else if ((-72 > lat) && (lat >= -80)) { - LetterDesignator = 'C'; +function getLetterDesignator(latitude) { + if (latitude <= 84 && latitude >= 72) { + // the X band is 12 degrees high + return 'X'; + } else if (latitude < 72 || latitude >= -80) { + // Latitude bands are lettered C through X, excluding I and O + const bandLetters = 'CDEFGHJKLMNPQRSTUVWX'; + const bandHeight = 8; + const minLatitude = -80; + const index = Math.floor((latitude - minLatitude) / bandHeight); + return bandLetters[index]; + } else if (latitude > 84 || latitude < -80) { + //This is here as an error flag to show that the Latitude is + //outside MGRS limits + return 'Z'; } - return LetterDesignator; } /**