Conversor_moneda/script.js

18 lines
577 B
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
}