Search code examples
javascriptloopscurrency-exchange-rates

JS: Iterate over page & convert US Dollars to Rands


I have a client website. Here are test login details Login: TestOne, Password: password.

I am trying to convert all values on the page into Dollars. I have changed some subscriptions to nominal values (Neo Natal: Month 0-1 & Neo Natal: Month 1-2) to a nominal value to test my function:

function callbackTester(callback, cstmClass) {
  var script = document.createElement('script');
  script.setAttribute('src', "http://query.yahooapis.com/v1/public/yql?q=select%20rate%2Cname%20from%20csv%20where%20url%3D'http%3A%2F%2Fdownload.finance.yahoo.com%2Fd%2Fquotes%3Fs%3DUSDZAR%253DX%26f%3Dl1n'%20and%20columns%3D'rate%2Cname'&format=json&callback=parseExchangeRate");
  document.body.appendChild(script);
}

function tryMe(cstmClass) {
  var name = data.query.results.row.name;
  var rate = Math.round(parseFloat(data.query.results.row.rate, 10));

  console.log('Rand - Dollar exchange rate: ' + rate);
  var priceConvert = document.getElementsByClassName(cstmClass);

  for (var i=0; i<priceConvert.length; i++) {
    var currentPrice = priceConvert[i].innerHTML;
    priceConvert[i].innerHTML = 'R' + (currentPrice * rate) + '.00';
  }
}

callbackTester(tryMe, 'price-convert');

I was trying to use this callback function format in the hopes that the classback function would iterate over the page:

function callbackTester (callback, param1, param2) {
    callback (param1, param2);
}

function tryMe (param1, param2) {
    alert (param1 + " and " + param2);
}

callbackTester (tryMe, "hello", "goodbye");

But I get the following error: ReferenceError: parseExchangeRate is not defined

...on the function: parseExchangeRate({"query":{"count":1,"created":"2017-07-10T14:30:37Z","lang":"en-GB","results":{"row":{"rate":"13.3762","name":"USD/ZAR"}}}});

I would appreciate any help you could offer!


Solution

  • your client website has jQuery then you can use it like

    function callbackTester(callback, cstmClass) {
        jQuery.ajax({
        jsonp: "callback",
        dataType: "jsonp",
        url: "https://query.yahooapis.com/v1/public/yql?q=select%20rate%2Cname%20from%20csv%20where%20url%3D'http%3A%2F%2Fdownload.finance.yahoo.com%2Fd%2Fquotes%3Fs%3DUSDZAR%253DX%26f%3Dl1n'%20and%20columns%3D'rate%2Cname'&format=json",
        success: function(data){
            console.log(data);
            callback(data, cstmClass);
        }
      });
    }
    
    function tryMe(data, cstmClass) {
      var name = data.query.results.row.name;
      var rate = Math.round(parseFloat(data.query.results.row.rate, 10));
    
      console.log('Rand - Dollar exchange rate: ' + rate);
      var priceConvert = document.getElementsByClassName(cstmClass);
    
      for (var i=0; i<priceConvert.length; i++) {
        var currentPrice = priceConvert[i].innerHTML;
        priceConvert[i].innerHTML = 'R' + (currentPrice * rate) + '.00';
      }
    }
    
    callbackTester(tryMe, 'price-convert');