Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

249
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda