Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

248
Visualizações
how can I use a Fetch variable - Javascript

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!!

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda