Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Manipulating API outputs with Javascript

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #35299
    Brightonmike
    Member

    I’m using the Google weather API for a forecast. It outputs temperatures in Fahrenheit and does not let you specify Celsius.

    The idea is to take the outputs, and then manipulate them using Javascript to turn them into Celsius.

    I’ve tried two methods, and neither have worked. The only way I can think to do it now is to somehow put the results into an array but I’m not sure how to do that.

    First Method
    This involved a function. However it was not manipulating each temperature separately but only manipulating the first. So all temps ended up the same…

    $(document).ready(function() {

    var ValOne = $('#Fahrenheit').val();
    var totalTotal = (((ValOne * 1)-32) * (5 * 1) / (9 *1));
    $('#Total').text(totalTotal);

    });
    echo ''. '
    Low
    ' . '
    ' . '
    °C
    ';

    Any ideas? This is the API http://www.google.com/ig/api?weather=bn424nt&hl=en

    #91162
    Brightonmike
    Member

    Had a stroke of genius. The API does supply the day of the week, so I can use that and append it to each field as an ID. This differentiates the fields, allowing the jQuery to treat them differently.

    I suspect it’s a bit of a work around – but it works!

    echo '' . '
    Low
    ' . '
    ' . '
    °F
    ';

    Ok, there are a couple of problems. First off my code is really bloated. I imagine I could definitely use an array now but not sure how.

    Also, sometimes the temperatures have too many digits. Tried the usual jquery function to remove digits but it won’t work. Here’s a linky to how far I’ve got:

    http://jsfiddle.net/brightonmike/uckA8/

    #91165
    Brightonmike
    Member

    Okay – even though my method above works, I’m barking up the wrong tree and didn’t need Javascript at all. PHP!

    echo '
    ' .round((($new->low* 1)-32) * (5 * 1) / (9 *1),1). '°C
    ';
    echo '
    ' .round((($new->high* 1)-32) * (5 * 1) / (9 *1),1). '°C
    ';
Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.