-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
52 lines (43 loc) · 1.7 KB
/
script.js
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
"use strict";
const label_from_currency = document.getElementById('from_currency');
const input_from_ammount = document.getElementById('from_ammount');
const label_to_currency = document.getElementById('to_currency');
const input_to_ammount = document.getElementById('to_ammount');
const tax_info = document.getElementById('tax_info');
const swap = document.getElementById('swap');
label_from_currency.addEventListener('change', calculate);
input_from_ammount.addEventListener('input', calculate);
label_to_currency.addEventListener('change', calculate);
input_to_ammount.addEventListener('input', calculate);
swap.addEventListener('click', infoSwap);
main();
function main() {
let currency = { "BRL": "Real", "EUR": "Euro", "USD": "Dollar" };
let options = [];
for (var [key, value] of Object.entries(currency)) {
options.push(`<option value='${key}'>${value}</option>`);
}
label_from_currency.innerHTML = options.join('\n');
label_to_currency.innerHTML = options.join('\n');
calculate();
}
function infoSwap() {
let temp = label_from_currency.value;
label_from_currency.value = label_to_currency.value;
label_to_currency.value = temp;
calculate();
}
async function getURL(url) {
return (await fetch(url)).json();
}
function getInfoSelect(select) {
return select.options[select.selectedIndex].text;
}
async function calculate() {
let from = label_from_currency.value;
let to = label_to_currency.value;
let { rates } = await getURL(`https://api.exchangerate-api.com/v4/latest/${from}`);
let rate = rates[to];
tax_info.innerText = `1 ${getInfoSelect(label_from_currency)} = ${rate} ${getInfoSelect(label_to_currency)}`
input_to_ammount.value = (input_from_ammount.value * rate).toFixed(2);
}