0

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>
9
  • Are you getting any errors in your console? Is the request being sent? What is its response status code? Commented Jan 9, 2020 at 17:44
  • 1
    When I hit that endpoint I get {"errors":{"key":"API quota exceeded"},"meta":{"dailyQuota":50,"requestCount":51}} Commented Jan 9, 2020 at 17:44
  • 5
    May have been a mistake to publish your API key to the entire world... Commented Jan 9, 2020 at 17:45
  • I made a snippet and added .fail(function(a,b,c) { console.log(a.responseJSON) }) it tells you all you need to know Commented Jan 9, 2020 at 17:47
  • 3
    Note that you're taking an argument named num, then immediately creating a new variable named num equal to something else... You're also not calling variconwndr24 anywhere. Commented Jan 9, 2020 at 17:47

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Ah, Thank you very much for your time and clarification! Much appreciated!
Just for the record, I'm fairly sure while (num >= 360) num -= 360; is equivalent (but possibly worse performance) to num = num % 360; Check out this jsfiddle here
Thanks @TKoL for the help so far. I ran the script and was able to get the console log for both the inconwndr24 and arr [Math.abs(val)]. When calling the data to the html page, is it better to call the var or arr?
I'm not entirely sure what you mean, but hopefully this answer is relevant: leave the degToCompass function alone: it takes a number, does some calculations, and returns another number, so don't do anything inside that function. Just use the result that gets returned from the function.
sorry about the confusion. I was referring to getting the variable iconwnndr24 to the HTML page.
|
0

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);

1 Comment

Thank you for taking the time to help. I really appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.