123456789101112131415161718192021222324252627282930313233343536373839404142 |
- <head>
- <h1>closure calc 2</h1>
- </head>
- <body>
- <select id='from'> </select>
- <select id='to'></select> <br>
- <div id='rate'></div><br>
- <input type='number' id='amount' /><br>
- <div id='result'></div>
- <script>
- fetch(`https://open.er-api.com/v6/latest/USD`)
- .then(response => response.json())
- .then(data => {
- let crossRates = {};
- for (const curr1 in data.rates) {
- let option = document.createElement("option");
- option.innerText = curr1;
- from.append(option);
- option = document.createElement("option");
- option.innerText = curr1;
- to.append(option);
- let curr1Rates = {};
- crossRates[curr1] = curr1Rates;
- for (const curr2 in data.rates) {
- curr1Rates[curr2] = data.rates[curr2] / data.rates[curr1];
- }
- }
- const calcResult = () => {
- let currFrom = from.value;
- let currTo = to.value;
- currVal = amount.value;
- rateVal = crossRates[currFrom][currTo];
- result.innerText = currVal * rateVal;
- rate.innerText = rateVal;
- }
- from.onchange = calcResult;
- to.onchange = calcResult;
- amount.oninput = calcResult;
- });
- </script>
- </body>
|