Getting a compass point from 140°Calculating compass directionFind number matching rangeCheck for boolean...

Quenching swords in dragon blood; why?

Cryptic with missing capitals

difference between two quite-similar Terminal commands

Program that converts a number to a letter of the alphabet

How to prevent cleaner from hanging my lock screen in Ubuntu 16.04

Blindfold battle as a gladiatorial spectacle - what are the tactics and communication methods?

Why does String.replaceAll() work differently in Java 8 from Java 9?

Lick explanation

What is better: yes / no radio, or simple checkbox?

Why does lambda auto& parameter choose const overload?

Eww, those bytes are gross

Can we use the stored gravitational potential energy of a building to produce power?

If I delete my router's history can my ISP still provide it to my parents?

What is the purpose of easy combat scenarios that don't need resource expenditure?

What kind of hardware implements Fourier transform?

Calculate Contact age in a Drupal view

Dilemma of explaining to interviewer that he is the reason for declining second interview

Strange blocking on readable secondary after reboot

My cat mixes up the floors in my building. How can I help him?

Slow moving projectiles from a hand-held weapon - how do they reach the target?

Why would the Pakistan airspace closure cancel flights not headed to Pakistan itself?

Parsing a string of key-value pairs as a dictionary

How to acknowledge an embarrassing job interview, now that I work directly with the interviewer?

Am I a Rude Number?



Getting a compass point from 140°


Calculating compass directionFind number matching rangeCheck for boolean values in an object and convert them to numbersIs there a way to make my Rock, Paper, Scissors game more concise?Is this a good way of writing custom tag attributes and rendering code?Using interface for generated functions or just create plain functionsNumerology CalculatorCopying a large number of DOM elementsRock, Paper, Scissors game in JavaScriptSearch object literal and return first key/value pair that matches regexShuntyard Javascript Calculator with unit testsCreating chart from data fetched from backend













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 2 days 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$









  • 7




    $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$









  • 7




    $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$









  • 7




    $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








  • 7




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














  • 7




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








7




7




$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

Fairchild Swearingen Metro Inhaltsverzeichnis Geschichte | Innenausstattung | Nutzung | Zwischenfälle...

Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...

Marineschifffahrtleitung Inhaltsverzeichnis Geschichte | Heutige Organisation der NATO | Nationale und...