-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurrencyScript.js
More file actions
67 lines (57 loc) · 2.26 KB
/
Copy pathcurrencyScript.js
File metadata and controls
67 lines (57 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
function displayCurrencies() {
$.ajax({
"async": true,
"crossDomain": true,
url: "https://currency-converter13.p.rapidapi.com/list",
method: 'GET',
"headers": {
"x-rapidapi-host": "currency-converter13.p.rapidapi.com",
"x-rapidapi-key": "48bf12c8d6msh0ac264695b8e047p12405fjsn64e8ea946e35",
"Access-Control-Allow-Origin": "https://nickkdb.github.io/Group5-Repository"
}
}).then(function (response) {
console.log(response);
response = response.sort();
for (var i = 0; i < response.length; i++) {
var currencyOption = $('<option>').text(response[i]);
if (response[i] === 'USD') {
currencyOption.attr('selected', 'selected');
}
$('#fromCurrency').append(currencyOption);
}
for (var i = 0; i < response.length; i++) {
var currencyOption = $('<option>').text(response[i]);
if (response[i] === 'EUR') {
currencyOption.attr('selected', 'selected');
}
$('#toCurrency').append(currencyOption);
}
})
$('#getCurrency').on('click', function () {
var fromCurrency = $('#fromCurrency option:selected').text();
var toCurrency = $('#toCurrency option:selected').text();
var queryString = '&from=' + fromCurrency + '&to=' + toCurrency;
console.log(queryString);
$.ajax({
"async": true,
"crossDomain": true,
url: "https://currency-converter13.p.rapidapi.com/convert?amount=1" + queryString,
method: 'GET',
"headers": {
"x-rapidapi-host": "currency-converter13.p.rapidapi.com",
"x-rapidapi-key": "48bf12c8d6msh0ac264695b8e047p12405fjsn64e8ea946e35"
}
}).then(function (response) {
console.log(response);
var exchange = $('<h4>');
var rounded = parseFloat(response.amount);
rounded = rounded.toFixed(2);
exchange.text(rounded);
var desc = $('<p>');
desc.text('For every 1 ' + fromCurrency + ', you get ~ ' + rounded + ' ' + toCurrency + ' in return.');
$('#displayCurrency').empty();
$('#displayCurrency').append(exchange);
$('#displayCurrency').append(desc);
})
})
}