task-12.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Задание на синий пояс
  2. // Сделать задания обмена валют используя ассоциативный массив (объект) подобной структуры. Добавьте дополнительные поля при надобности. Для обращения к нужному полю используйте [].
  3. // let ratios = {
  4. // usd: 25.6,
  5. // eur: 29
  6. // }
  7. // real data
  8. // Иcпользуя заготовку ниже реализуйте перевод валют с реальными данными.
  9. // fetch('https://open.er-api.com/v6/latest/USD').then(res => res.json())
  10. // .then(data => {
  11. // console.log(data.rates.UAH)
  12. // })
  13. const f12 = (rootId) => {
  14. const task12block = document.createElement('div');
  15. const task12_1title = document.createElement('h2');
  16. task12_1title.innerText = 'Task-12-1 Сurrency calc blue belt';
  17. const exchange_12_1Btn = document.createElement('button');
  18. exchange_12_1Btn.innerText = "Exchange UAH to USD or EUR";
  19. exchange_12_1Btn.style = 'margin-bottom:20px';
  20. rootId.appendChild(task12block);
  21. task12block.appendChild(task12_1title);
  22. task12block.appendChild(exchange_12_1Btn);
  23. let ratios = {
  24. usd: 25.6,
  25. eur: 29
  26. }
  27. exchange_12_1Btn.onclick = () => {
  28. const toCurrency = prompt('Введите валюту, которую хотите купить: EUR или USD');
  29. let fromAmount=null;
  30. let toAmount = null;
  31. if (toCurrency) {
  32. switch (toCurrency.toLowerCase()) {
  33. case 'usd':
  34. fromAmount = prompt('Введите сумму в гривне');
  35. toAmount = fromAmount / ratios['usd'];
  36. break;
  37. case 'eur':
  38. fromAmount = prompt('Введите сумму в гривне');
  39. toAmount = fromAmount / ratios['eur'];
  40. break;
  41. default: alert('Чет я не понял, попробуйте еще раз');
  42. break;
  43. }
  44. !fromAmount || alert(`На ${fromAmount} UAH вы можете приобрести ${Math.round(toAmount * 100) / 100} ${toCurrency.toUpperCase()} `);
  45. }
  46. else alert('Чет я не понял, попробуйте еще раз');
  47. }
  48. const task12_2title = document.createElement('h2');
  49. task12_2title.innerText = 'Task-12-2 Сurrency calc: real data';
  50. const exchange_12_2Btn = document.createElement('button');
  51. exchange_12_2Btn.innerText = "Exchange UAH to USD or EUR";
  52. exchange_12_2Btn.style = 'margin-bottom:20px';
  53. task12block.appendChild(task12_2title);
  54. task12block.appendChild(exchange_12_2Btn);
  55. exchange_12_2Btn.onclick = () => {
  56. let toCurrency = prompt('Введите валюту, которую хотите купить: EUR или USD');
  57. let fromAmount=null;
  58. let toAmount = null;
  59. let currencyRatio = null;
  60. if (toCurrency) {
  61. toCurrency = toCurrency.toLowerCase();
  62. if (toCurrency === 'usd' || toCurrency === 'eur') {
  63. fromAmount = prompt('Введите сумму в гривне');
  64. fetch(`https://open.er-api.com/v6/latest/${toCurrency}`).then(res => res.json())
  65. .then(data => {
  66. toAmount = fromAmount / (data.rates.UAH);
  67. !fromAmount || alert(`На ${fromAmount} UAH вы можете приобрести ${Math.round(toAmount * 100) / 100} ${toCurrency.toUpperCase()} `)
  68. })
  69. }
  70. else alert('Чет я не понял, попробуйте еще раз');
  71. }
  72. else alert('Чет я не понял, попробуйте еще раз');
  73. }
  74. }
  75. f12(root);
  76. export default f12;