script.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. let currency;
  2. const wrapper = document.querySelector('.cur__wrapper');
  3. //confirm('You want to see all currency names?') ? showCurencys() : exchange();
  4. function exchange() {
  5. ///currency = prompt('enter currency for exchange (usd, eur, rub...etc)');
  6. let url = 'https://open.er-api.com/v6/latest/' + currency.toUpperCase();
  7. if (currency.toLowerCase() === 'usd' || currency.toLowerCase() === 'eur' || currency.toLowerCase() === 'rub') {
  8. fetch(url)
  9. .then(res => res.json())
  10. .then(data => {
  11. let sum = prompt('enter amount');
  12. alert(`You usd summ : ${sum * data.rates.UAH} uah`);
  13. });
  14. } else if (currency) {
  15. fetch(url)
  16. .then(res => res.json())
  17. .then(data => {
  18. let sum = prompt('enter amount');
  19. alert(`You usd summ : ${sum * data.rates.UAH} uah`);
  20. });
  21. } else {
  22. alert('Enter correct currency name');
  23. }
  24. };
  25. function showCurencys() {
  26. fetch('https://open.er-api.com/v6/latest').then(res => res.json())
  27. .then(data => {
  28. for (let key in data.rates) {
  29. let div = document.createElement('div');
  30. wrapper.appendChild(div);
  31. div.innerHTML =`<button class='btn_cur'> ${key}</button>`;
  32. }
  33. exchange();
  34. });
  35. }
  36. showCurencys();