Conversor_moneda/script.js

53 lines
1.4 KiB
JavaScript
Raw Normal View History

2020-05-30 12:11:35 +01:00
async function api(base){
const res = await fetch(`https://api.exchangeratesapi.io/latest?base=${base}`);
const data = await res.json();
return data.rates;
}
async function convert(){
const base = document.getElementById('curr').value;
const rates = await api(base);
const cur = document.getElementById('curr2').value;
var coef = rates[cur];
if (base==cur){
coef = 1;
}
var val = document.getElementById('val').value;
document.getElementById('res').textContent = Number((val*coef).toFixed(2)).toLocaleString('en') + ' ' +document.getElementById('curr2').value;
2020-05-30 12:11:35 +01:00
}
2020-06-21 23:58:58 +01:00
async function convert2(){
const base = document.getElementById('rev').value;
const rates = await api(base);
const cur = document.getElementById('rev2').value;
var coef = rates[cur];
if (base==cur){
coef = 1;
}
var val = document.getElementById('val2').value;
document.getElementById('res2').textContent = Number((val*coef).toFixed(2)).toLocaleString('en') + ' ' +document.getElementById('rev2').value;
}
const from1 = document.getElementById('curr');
const to1 = document.getElementById('curr2');
const from2 = document.getElementById('rev');
const to2 = document.getElementById('rev2');
from1.onchange = function(a){
to2.value = a.target.value;
};
to1.onchange = function(a){
from2.value = a.target.value;
};
from2.onchange = function(a){
to1.value = a.target.value;
};
to2.onchange = function(a){
from1.value = a.target.value;
};