Skip to content

Instantly share code, notes, and snippets.

@Shivabeach
Last active July 27, 2021 15:29
Show Gist options
  • Save Shivabeach/05585b99501e3ab5e27c12ea7adf13d5 to your computer and use it in GitHub Desktop.
Save Shivabeach/05585b99501e3ab5e27c12ea7adf13d5 to your computer and use it in GitHub Desktop.
Open #weathermap conversion from degrees to direction. #degrees #direction #javascript
$.getJSON("https://api.openweathermap.org/data/2.5/weather?id=4614214&units=imperial&APPID=openweathermap api code", function(data) {
var icon = "http://openweathermap.org/img/w/" + data.weather[0].icon + ".png";
var temp = Math.floor(data.main.temp);
var weather = data.weather[0].description;
var humid = data.main.humidity;
var windspeed = data.wind.speed + " mph";
var deg = Math.floor(data.wind.deg);
switch (true) {
case deg >= 360 && deg <= 21:
deg = "N";
break;
case deg >= 22 && deg <= 44:
deg = "NNE";
break;
case deg >= 45 && deg <= 66:
deg = "NE";
break;
case deg >= 67 && deg <= 89:
deg = "ENE";
break;
case deg >= 90 && deg <= 111:
deg = "E";
break;
case deg >= 112 && deg <= 134:
deg = "ESE";
break;
case deg >= 135 && deg <= 156:
deg = "SE";
break;
case deg >= 157 && deg <= 179:
deg = "SSE";
break;
case deg >= 180 && deg <= 201:
deg = "S";
break;
case deg >= 202 && deg <= 224:
deg = "SSW";
break;
case deg >= 225 && deg <= 246:
deg = "SW";
break;
case deg >= 247 && deg <= 269:
deg = "WSW";
break;
case deg >= 270 && deg <= 291:
deg = "W";
break;
case deg >= 292 && deg <= 314:
deg = "WNW";
break;
case deg >= 315 && deg <= 336:
deg = "NW";
break;
case deg >= 337 && deg <= 359:
deg = "NNW";
break;
default:
deg = "no data";
}
@Pheebzer
Copy link

case deg >= 360 && deg <= 21:

The degree can't be both bigger than 360 and smaller than 21 at the same time.

You should switch the "and" operator (&&) to "or" operator (||) in order to make this work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment