Reputation: 47
I wrote this script based on information I read here on Stack. It calls data from an API and is supposed to convert the directional degrees to cardinal. When I run it, I get no output. There is no error when I inspect the page. I found no syntax errors when I ran it through Fiddle.
I thought I could simply substitute a number (I tried 45) for num and get the script to run to no avail so I could use an expert eye. Thank you.
var settings = {
"url": "https://api.stormglass.io/v1/weather/point?lat=40.370181&lng=-73.934193&key=...",
"method": "GET",
"timeout": 0,
};
$.ajax(settings)
.fail(function(a,b,c) { console.log(a.responseJSON) })
.done(function(response) {
console.log(response);
variconwndr24 = function degToCompass(num) {
var num = response.hours[17].windDirection[1].value;;
while (num < 0) num += 360;
while (num >= 360) num -= 360;
val = Math.round((num - 11.25) / 22.5);
arr = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE",
"SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"
];
return arr[Math.abs(val)];
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Upvotes: 0
Views: 142
Reputation: 1120
The main problem is in the callback function for done. You're defining a function degToCompass
, but that function is never called. Additionally, you're re-defining a parameter for the function that you passed. No point in passing num as a parameter if you just overwrite it. Instead just pass response's desired value as the parameter. Also, for the sake of readability and maintenance, try breaking this up like this :
const arr = [
"N",
"NNE",
"NE",
"ENE",
"E",
"ESE",
"SE",
"SSE",
"S",
"SSW",
"SW",
"WSW",
"W",
"WNW",
"NW",
"NNW"
];
var settings =
{
"url": "https://api.stormglass.io/v1/weather/point?lat=40.370181&lng=-73.934193&key=...",
"method": "GET",
"timeout": 0,
};
function degToCompass(num)
{
while (num < 0)
{
num += 360;
}
while (num >= 360)
{
num -= 360;
}
val = Math.round((num - 11.25) / 22.5);
return arr[Math.abs(val)];
}
function faiureCallback(a, b, c)
{
console.log(a.responseJSON);
}
function doneCallback(response)
{
// Do some stuff
var num = response.hours[17].windDirection[1].value;;
return degToCompass(num)
}
$.ajax(settings)
.fail(faiureCallback)
.done(doneCallback);
Upvotes: 0
Reputation: 13892
First off, try something like this instead:
var settings = {
"url": "https://api.stormglass.io/v1/weather/point?lat=40.370181&lng=-73.934193&key=xdvfd",
"method": "GET",
"timeout": 0,
};
$.ajax(settings).done(function(response) {
console.log(response);
var degrees = response.hours[17].windDirection[1].value;
variconwndr24 = degToCompass(degrees);
console.log(variconwndr24);
return variconwndr24;
});
function degToCompass(num) {
while (num < 0) num += 360;
while (num >= 360) num -= 360;
val = Math.round((num - 11.25) / 22.5);
arr = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE",
"SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"
];
console.log(arr[Math.abs(val)]);
return arr[Math.abs(val)];
}
In the way you did it, degToCompass never actually gets called, and the num
argument becomes redundant because you immediately redefine it.
Upvotes: 2