Conversor_moneda/script.js

28 lines
740 B
JavaScript
Raw Normal View History

2020-05-30 12:11:35 +01:00
async function api(base){
2023-07-06 21:31:44 +01:00
const res = await fetch(`https://v6.exchangerate-api.com/v6/aa74a76fa349403ebf36b1ff/latest/${base}`);
2020-05-30 12:11:35 +01:00
const data = await res.json();
2023-07-06 21:31:44 +01:00
return data.conversion_rates;
2020-05-30 12:11:35 +01:00
}
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;
2023-07-06 21:31:44 +01:00
document.getElementById('res').value = Number((val*coef).toFixed(2)).toLocaleString('en');
2020-06-21 23:58:58 +01:00
}
const from1 = document.getElementById('curr');
const to1 = document.getElementById('curr2');
2023-07-06 21:31:44 +01:00
function change(){
tmp = from1.value;
from1.value = to1.value;
to1.value = tmp;
}
2020-06-21 23:58:58 +01:00