hw09_07closure calc 2.html 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <head>
  2. <h1>closure calc 2</h1>
  3. </head>
  4. <body>
  5. <select id='from'> </select>
  6. <select id='to'></select> <br>
  7. <div id='rate'></div><br>
  8. <input type='number' id='amount' /><br>
  9. <div id='result'></div>
  10. <script>
  11. fetch(`https://open.er-api.com/v6/latest/USD`)
  12. .then(response => response.json())
  13. .then(data => {
  14. let crossRates = {};
  15. for (const curr1 in data.rates) {
  16. let option = document.createElement("option");
  17. option.innerText = curr1;
  18. from.append(option);
  19. option = document.createElement("option");
  20. option.innerText = curr1;
  21. to.append(option);
  22. let curr1Rates = {};
  23. crossRates[curr1] = curr1Rates;
  24. for (const curr2 in data.rates) {
  25. curr1Rates[curr2] = data.rates[curr2] / data.rates[curr1];
  26. }
  27. }
  28. const calcResult = () => {
  29. let currFrom = from.value;
  30. let currTo = to.value;
  31. currVal = amount.value;
  32. rateVal = crossRates[currFrom][currTo];
  33. result.innerText = currVal * rateVal;
  34. rate.innerText = rateVal;
  35. }
  36. from.onchange = calcResult;
  37. to.onchange = calcResult;
  38. amount.oninput = calcResult;
  39. });
  40. </script>
  41. </body>