Getting a compass point from 140°












3












$begingroup$


This JavaScript converts degrees to a compass point, e.g. convert.toCompass(140) gets 140° -> SE.



I can't help but think: is there a more concise way to do this?



var convert = {
toCompass: function(degrees)
{
if(degrees >= 0 && degrees <= 11.25)
{
return 'N';
}

else if(degrees > 11.25 && degrees <= 33.75)
{
return 'NNE';
}

else if(degrees > 33.75 && degrees <= 56.25)
{
return 'NE';
}

else if(degrees > 56.25 && degrees <= 78.75)
{
return 'ENE';
}

else if(degrees > 78.75 && degrees <= 101.25)
{
return 'E';
}

else if(degrees > 101.25 && degrees <= 123.75)
{
return 'ESE';
}

else if(degrees > 123.75 && degrees <= 146.25)
{
return 'SE';
}

else if(degrees > 146.25 && degrees <= 168.75)
{
return 'SSE';
}

else if(degrees > 168.75 && degrees <= 191.25)
{
return 'S';
}

else if(degrees > 191.25 && degrees <= 213.75)
{
return 'SSW';
}

else if(degrees > 213.75 && degrees <= 236.25)
{
return 'SW';
}

else if(degrees > 236.25 && degrees <= 258.75)
{
return 'WSW';
}

else if(degrees > 258.75 && degrees <= 281.25)
{
return 'W';
}

else if(degrees > 281.25 && degrees <= 303.75)
{
return 'WNW';
}

else if(degrees > 303.75 && degrees <= 326.25)
{
return 'NW';
}

else if(degrees > 326.25 && degrees <= 348.75)
{
return 'NNW';
}

else if(degrees > 348.75 && degrees <= 360)
{
return 'N';
}
}
}









share|improve this question











$endgroup$












  • $begingroup$
    THis question is very similar to another question here. There is a fair amount of discussion on that other question whihc may be useful to you too: codereview.stackexchange.com/questions/36744/…
    $endgroup$
    – rolfl
    Jan 5 '14 at 12:25
















3












$begingroup$


This JavaScript converts degrees to a compass point, e.g. convert.toCompass(140) gets 140° -> SE.



I can't help but think: is there a more concise way to do this?



var convert = {
toCompass: function(degrees)
{
if(degrees >= 0 && degrees <= 11.25)
{
return 'N';
}

else if(degrees > 11.25 && degrees <= 33.75)
{
return 'NNE';
}

else if(degrees > 33.75 && degrees <= 56.25)
{
return 'NE';
}

else if(degrees > 56.25 && degrees <= 78.75)
{
return 'ENE';
}

else if(degrees > 78.75 && degrees <= 101.25)
{
return 'E';
}

else if(degrees > 101.25 && degrees <= 123.75)
{
return 'ESE';
}

else if(degrees > 123.75 && degrees <= 146.25)
{
return 'SE';
}

else if(degrees > 146.25 && degrees <= 168.75)
{
return 'SSE';
}

else if(degrees > 168.75 && degrees <= 191.25)
{
return 'S';
}

else if(degrees > 191.25 && degrees <= 213.75)
{
return 'SSW';
}

else if(degrees > 213.75 && degrees <= 236.25)
{
return 'SW';
}

else if(degrees > 236.25 && degrees <= 258.75)
{
return 'WSW';
}

else if(degrees > 258.75 && degrees <= 281.25)
{
return 'W';
}

else if(degrees > 281.25 && degrees <= 303.75)
{
return 'WNW';
}

else if(degrees > 303.75 && degrees <= 326.25)
{
return 'NW';
}

else if(degrees > 326.25 && degrees <= 348.75)
{
return 'NNW';
}

else if(degrees > 348.75 && degrees <= 360)
{
return 'N';
}
}
}









share|improve this question











$endgroup$












  • $begingroup$
    THis question is very similar to another question here. There is a fair amount of discussion on that other question whihc may be useful to you too: codereview.stackexchange.com/questions/36744/…
    $endgroup$
    – rolfl
    Jan 5 '14 at 12:25














3












3








3


1



$begingroup$


This JavaScript converts degrees to a compass point, e.g. convert.toCompass(140) gets 140° -> SE.



I can't help but think: is there a more concise way to do this?



var convert = {
toCompass: function(degrees)
{
if(degrees >= 0 && degrees <= 11.25)
{
return 'N';
}

else if(degrees > 11.25 && degrees <= 33.75)
{
return 'NNE';
}

else if(degrees > 33.75 && degrees <= 56.25)
{
return 'NE';
}

else if(degrees > 56.25 && degrees <= 78.75)
{
return 'ENE';
}

else if(degrees > 78.75 && degrees <= 101.25)
{
return 'E';
}

else if(degrees > 101.25 && degrees <= 123.75)
{
return 'ESE';
}

else if(degrees > 123.75 && degrees <= 146.25)
{
return 'SE';
}

else if(degrees > 146.25 && degrees <= 168.75)
{
return 'SSE';
}

else if(degrees > 168.75 && degrees <= 191.25)
{
return 'S';
}

else if(degrees > 191.25 && degrees <= 213.75)
{
return 'SSW';
}

else if(degrees > 213.75 && degrees <= 236.25)
{
return 'SW';
}

else if(degrees > 236.25 && degrees <= 258.75)
{
return 'WSW';
}

else if(degrees > 258.75 && degrees <= 281.25)
{
return 'W';
}

else if(degrees > 281.25 && degrees <= 303.75)
{
return 'WNW';
}

else if(degrees > 303.75 && degrees <= 326.25)
{
return 'NW';
}

else if(degrees > 326.25 && degrees <= 348.75)
{
return 'NNW';
}

else if(degrees > 348.75 && degrees <= 360)
{
return 'N';
}
}
}









share|improve this question











$endgroup$




This JavaScript converts degrees to a compass point, e.g. convert.toCompass(140) gets 140° -> SE.



I can't help but think: is there a more concise way to do this?



var convert = {
toCompass: function(degrees)
{
if(degrees >= 0 && degrees <= 11.25)
{
return 'N';
}

else if(degrees > 11.25 && degrees <= 33.75)
{
return 'NNE';
}

else if(degrees > 33.75 && degrees <= 56.25)
{
return 'NE';
}

else if(degrees > 56.25 && degrees <= 78.75)
{
return 'ENE';
}

else if(degrees > 78.75 && degrees <= 101.25)
{
return 'E';
}

else if(degrees > 101.25 && degrees <= 123.75)
{
return 'ESE';
}

else if(degrees > 123.75 && degrees <= 146.25)
{
return 'SE';
}

else if(degrees > 146.25 && degrees <= 168.75)
{
return 'SSE';
}

else if(degrees > 168.75 && degrees <= 191.25)
{
return 'S';
}

else if(degrees > 191.25 && degrees <= 213.75)
{
return 'SSW';
}

else if(degrees > 213.75 && degrees <= 236.25)
{
return 'SW';
}

else if(degrees > 236.25 && degrees <= 258.75)
{
return 'WSW';
}

else if(degrees > 258.75 && degrees <= 281.25)
{
return 'W';
}

else if(degrees > 281.25 && degrees <= 303.75)
{
return 'WNW';
}

else if(degrees > 303.75 && degrees <= 326.25)
{
return 'NW';
}

else if(degrees > 326.25 && degrees <= 348.75)
{
return 'NNW';
}

else if(degrees > 348.75 && degrees <= 360)
{
return 'N';
}
}
}






javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 22 mins ago









Jamal

30.3k11119227




30.3k11119227










asked Jan 5 '14 at 8:34









Danny BeckettDanny Beckett

1255




1255












  • $begingroup$
    THis question is very similar to another question here. There is a fair amount of discussion on that other question whihc may be useful to you too: codereview.stackexchange.com/questions/36744/…
    $endgroup$
    – rolfl
    Jan 5 '14 at 12:25


















  • $begingroup$
    THis question is very similar to another question here. There is a fair amount of discussion on that other question whihc may be useful to you too: codereview.stackexchange.com/questions/36744/…
    $endgroup$
    – rolfl
    Jan 5 '14 at 12:25
















$begingroup$
THis question is very similar to another question here. There is a fair amount of discussion on that other question whihc may be useful to you too: codereview.stackexchange.com/questions/36744/…
$endgroup$
– rolfl
Jan 5 '14 at 12:25




$begingroup$
THis question is very similar to another question here. There is a fair amount of discussion on that other question whihc may be useful to you too: codereview.stackexchange.com/questions/36744/…
$endgroup$
– rolfl
Jan 5 '14 at 12:25










1 Answer
1






active

oldest

votes


















7












$begingroup$

By looking at values in your series of if's, you can make an array (), then calculate which index to pick:



var convert = {
toCompass: function(degrees)
{
return ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N'][Math.round(degrees / 11.25 / 2)];
}
}

alert(convert.toCompass(140)); // SE


JSFiddle demo






share|improve this answer











$endgroup$









  • 5




    $begingroup$
    I'd throw in a % 360 to be defensive.
    $endgroup$
    – 200_success
    Jan 5 '14 at 9:22











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f38613%2fgetting-a-compass-point-from-140%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









7












$begingroup$

By looking at values in your series of if's, you can make an array (), then calculate which index to pick:



var convert = {
toCompass: function(degrees)
{
return ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N'][Math.round(degrees / 11.25 / 2)];
}
}

alert(convert.toCompass(140)); // SE


JSFiddle demo






share|improve this answer











$endgroup$









  • 5




    $begingroup$
    I'd throw in a % 360 to be defensive.
    $endgroup$
    – 200_success
    Jan 5 '14 at 9:22
















7












$begingroup$

By looking at values in your series of if's, you can make an array (), then calculate which index to pick:



var convert = {
toCompass: function(degrees)
{
return ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N'][Math.round(degrees / 11.25 / 2)];
}
}

alert(convert.toCompass(140)); // SE


JSFiddle demo






share|improve this answer











$endgroup$









  • 5




    $begingroup$
    I'd throw in a % 360 to be defensive.
    $endgroup$
    – 200_success
    Jan 5 '14 at 9:22














7












7








7





$begingroup$

By looking at values in your series of if's, you can make an array (), then calculate which index to pick:



var convert = {
toCompass: function(degrees)
{
return ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N'][Math.round(degrees / 11.25 / 2)];
}
}

alert(convert.toCompass(140)); // SE


JSFiddle demo






share|improve this answer











$endgroup$



By looking at values in your series of if's, you can make an array (), then calculate which index to pick:



var convert = {
toCompass: function(degrees)
{
return ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N'][Math.round(degrees / 11.25 / 2)];
}
}

alert(convert.toCompass(140)); // SE


JSFiddle demo







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 5 '14 at 9:12









Danny Beckett

1255




1255










answered Jan 5 '14 at 8:52









user34483user34483

863




863








  • 5




    $begingroup$
    I'd throw in a % 360 to be defensive.
    $endgroup$
    – 200_success
    Jan 5 '14 at 9:22














  • 5




    $begingroup$
    I'd throw in a % 360 to be defensive.
    $endgroup$
    – 200_success
    Jan 5 '14 at 9:22








5




5




$begingroup$
I'd throw in a % 360 to be defensive.
$endgroup$
– 200_success
Jan 5 '14 at 9:22




$begingroup$
I'd throw in a % 360 to be defensive.
$endgroup$
– 200_success
Jan 5 '14 at 9:22


















draft saved

draft discarded




















































Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f38613%2fgetting-a-compass-point-from-140%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Сан-Квентин

8-я гвардейская общевойсковая армия

Алькесар