19_real_data.js 858 B

12345678910111213141516171819202122232425262728293031
  1. let currency = prompt('Укажите валюту:');
  2. let exchangeRateSymbol = '';
  3. if(currency) {
  4. currency = currency.toLowerCase();
  5. if(currency === 'usd') {
  6. exchangeRateSymbol = '$';
  7. } else if(currency === 'eur') {
  8. exchangeRateSymbol = '€';
  9. } else {
  10. alert('Неверно указанная валюта');
  11. }
  12. } else {
  13. alert('Укажите валюту:');
  14. }
  15. if(currency === 'usd' || currency === 'eur' ) {
  16. let quantity = +prompt('Укажите величину в гривне для обмена');
  17. const fetchExchangeRate = (currency) => {
  18. fetch(`https://open.er-api.com/v6/latest/${currency}`)
  19. .then(res => res.json())
  20. .then(data => {
  21. alert(`${quantity} грн в ${currency} = ${(quantity / data.rates.UAH).toFixed(2)}${exchangeRateSymbol}`);
  22. });
  23. }
  24. fetchExchangeRate(currency);
  25. }