Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

245
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!