Created
March 3, 2016 13:08
-
-
Save beovulf/ba4d33464ccefb95c463 to your computer and use it in GitHub Desktop.
currency converter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var numberPattern = /\d+/g; | |
var currencyType = /(GBP)|(PLN)|(USD)|(EUR)/g; | |
function httpGet(theUrl){ | |
var xmlHttp = null; | |
xmlHttp = new XMLHttpRequest(); | |
xmlHttp.open( "GET", theUrl, false ); | |
xmlHttp.send( null ); | |
return xmlHttp.responseText; | |
} | |
function currencyConverter(currency_from,currency_to,currency_input){ | |
var yql_base_url = "https://query.yahooapis.com/v1/public/yql"; | |
var yql_query = 'select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20("'+currency_from+currency_to+'")'; | |
var yql_query_url = yql_base_url + "?q=" + yql_query + "&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"; | |
var http_response = httpGet(yql_query_url); | |
var http_response_json = JSON.parse(http_response); | |
return http_response_json.query.results.rate.Rate; | |
} | |
var currency_input = 5; | |
var currency_to = "PLN"; | |
var usd= currencyConverter("USD",currency_to,currency_input); | |
var gbp = currencyConverter("GBP",currency_to,currency_input); | |
var eur = currencyConverter("EUR",currency_to,currency_input); | |
$('.offer-price').each(function(){ | |
var getText = $(this).text(); | |
var value = (parseInt(getText.match( numberPattern ),10)); | |
var currency = getText.match(currencyType); | |
if (currency == "GBP") { | |
$(this).text('od '+(Math.round(value*gbp))+' PLN' ) | |
} else if (currency == "USD") { | |
$(this).text('od '+(Math.round(value*usd))+' PLN' ) | |
} else if (currency == "EUR") { | |
$(this).text('od '+(Math.round(value*eur))+' PLN' ) | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment