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