I have this Fetch code to get the data of the coins.
function getCurrency() {
const currencySelected = currencySelect.value;
fetch(`https://api.exchangerate-api.com/v4/latest/${currencySelected}`)
.then(res => res.json())
.then(data => {
let rates = data.rates;
})
.catch(error => console.error('Error:', error));
}
I also have this code to change the currencies as the user chooses, but it tells me that the variable 'rates' does not exist. How can I make the variable usable in all the code?
//Change currency
function changeCurrency(){
let oldUnit = "USD";
currencySelect.onchange = function (e) {
let newUnit = currencySelect.value;
document.querySelectorAll('.movieOption').forEach((movie)=> {
let newValue = movie.value * rates[newUnit]/rates[oldUnit];
let newText = `${movie.dataset.title} ${newValue.toFixed(2)} ${newUnit}`
movie.textContent = newText;
movie.value = newValue;
});
oldUnit = newUnit;
}
}
Thanks!!