65 lines
No EOL
1.4 KiB
JavaScript
65 lines
No EOL
1.4 KiB
JavaScript
const form = document.getElementById('convert');
|
|
|
|
const values = {
|
|
'Quadrans': 1,
|
|
'Semis': 2,
|
|
'As': 4,
|
|
'Dupondius': 8,
|
|
'Sestertius': 16,
|
|
'Quinarius argenteus': 32,
|
|
'Denarius': 64,
|
|
'Quinarius aureus': 800,
|
|
'Aureus': 1600,
|
|
}
|
|
|
|
function gcd(a,b) {
|
|
if(a==0) {
|
|
return 1;
|
|
}
|
|
console.log(a,b);
|
|
if(a<b) {
|
|
return gcd(b,a);
|
|
}
|
|
while(b) {
|
|
const [m,c] = [a%b,b];
|
|
a = c;
|
|
b = m;
|
|
}
|
|
return a;
|
|
}
|
|
|
|
function update() {
|
|
const fd = new FormData(form);
|
|
const {num_from, from, to} = Object.fromEntries(Array.from(fd));
|
|
console.log(num_from,from,to);
|
|
|
|
const value_from = values[from];
|
|
const value_to = values[to];
|
|
let [n,d] = value_to > value_from ? [num_from, value_to/value_from] : [num_from * value_from/value_to, 1];
|
|
const g = gcd(n,d);
|
|
n /= g;
|
|
d /= g;
|
|
const output = d==1 ? n : `${n} / ${d}`;
|
|
document.querySelector('output').textContent = output;
|
|
}
|
|
|
|
function swap() {
|
|
const fd = new FormData(form);
|
|
const {num_from, from, to} = Object.fromEntries(Array.from(fd));
|
|
|
|
const value_from = values[from];
|
|
const value_to = values[to];
|
|
|
|
document.getElementById('num_from').value = num_from * value_from/value_to;
|
|
|
|
document.getElementById('from').value = to;
|
|
document.getElementById('to').value = from;
|
|
|
|
update();
|
|
}
|
|
|
|
update();
|
|
|
|
form.addEventListener('input', update);
|
|
|
|
document.getElementById('swap').addEventListener('click', swap); |