add reverse conversion

This commit is contained in:
Xaloc 2020-06-22 00:58:58 +02:00
parent 600a5974b6
commit fe77a1cee0
2 changed files with 71 additions and 2 deletions

View File

@ -7,7 +7,8 @@
</head>
<body>
<select id='curr'>
<h3>Conversion</h3>
From: <select id='curr'>
<option value='EUR'>EUR</option>
<option value='USD'>USD</option>
<option value='SEK'>SEK</option>
@ -22,7 +23,7 @@
<input type='number' id='val' step='0.01'>
<select id='curr2'>
To: <select id='curr2'>
<option value='USD'>USD</option>
<option value='EUR'>EUR</option>
<option value='SEK'>SEK</option>
@ -40,7 +41,40 @@
<p>
Conversion: <span id='res'></span>
</p>
<h3>Reversed conversion</h3>
From: <select id='rev'>
<option value='USD'>USD</option>
<option value='EUR'>EUR</option>
<option value='SEK'>SEK</option>
<option value='JPY'>JPY</option>
<option value='GBP'>GBP</option>
<option value='INR'>INR</option>
<option value='ISK'>ISK</option>
<option value='DKK'>DKK</option>
<option value='RON'>RON</option>
<option value='NOK'>NOK</option>
</select>
<input type='number' id='val2' step='0.01'>
To: <select id='rev2'>
<option value='EUR'>EUR</option>
<option value='USD'>USD</option>
<option value='SEK'>SEK</option>
<option value='JPY'>JPY</option>
<option value='GBP'>GBP</option>
<option value='INR'>INR</option>
<option value='ISK'>ISK</option>
<option value='DKK'>DKK</option>
<option value='RON'>RON</option>
<option value='NOK'>NOK</option>
</select>
<button onClick='convert2()'>Convert</button>
<p>
Conversion: <span id='res2'></span>
</p>
<script src='script.js'></script>
</body>

View File

@ -15,3 +15,38 @@ async function convert(){
var val = document.getElementById('val').value;
document.getElementById('res').textContent = Number((val*coef).toFixed(2)).toLocaleString('en') + ' ' +document.getElementById('curr2').value;
}
async function convert2(){
const base = document.getElementById('rev').value;
const rates = await api(base);
const cur = document.getElementById('rev2').value;
var coef = rates[cur];
if (base==cur){
coef = 1;
}
var val = document.getElementById('val2').value;
document.getElementById('res2').textContent = Number((val*coef).toFixed(2)).toLocaleString('en') + ' ' +document.getElementById('rev2').value;
}
const from1 = document.getElementById('curr');
const to1 = document.getElementById('curr2');
const from2 = document.getElementById('rev');
const to2 = document.getElementById('rev2');
from1.onchange = function(a){
to2.value = a.target.value;
};
to1.onchange = function(a){
from2.value = a.target.value;
};
from2.onchange = function(a){
to1.value = a.target.value;
};
to2.onchange = function(a){
from1.value = a.target.value;
};