Unit_conversions
This is a Quarto website.
{
const div = html`<div>`;
let input_energy = document.getElementById("energy");
input_energy.addEventListener("keyup", (event) => update("energy"));
let energy_units = document.getElementById("energy_units");
energy_units.addEventListener("change", (event) => update_units("energy_units"));
let input_freqTHz = document.getElementById("freqTHz");
input_freqTHz.addEventListener("keyup", (event) => update("freqTHz"));
let freq_units = document.getElementById("freq_units");
freq_units.addEventListener("change", (event) => update_units("freq_units"));
let input_wavenumber = document.getElementById("wavenumber");
input_wavenumber.addEventListener("keyup", (event) => update("wavenumber"));
let input_wavelength = document.getElementById("wavelength");
input_wavelength.addEventListener("keyup", (event) => update("wavelength"));
let energy_unit = document.getElementById("energy_units");
energy_unit.addEventListener("change",(event) => console.log(energy_unit.value));
function update_units(f) {
if (f=="freq_units"){
update("freqTHz");
} else if (f=="energy_units"){
update("energy");
}
};
function update(e) {
let consts = constants;
let energy = document.getElementById("energy");
let thz = document.getElementById("freqTHz");
let wavenumber = document.getElementById("wavenumber");
let wavelength = document.getElementById("wavelength");
let freq_units = document.getElementById("freq_units");
let energy_units = document.getElementById("energy_units");
let doc_array = [energy,thz,wavenumber,wavelength];
let num_wl = 0;
let index = 0;
if (e=="energy"){
num_wl = (consts.h * consts.c / (energy.value * consts.e * Number(energy_units.value)) * 1e6);
index = 0;
} else if (e=="freqTHz") {
num_wl = (consts.c / (thz.value * Number(freq_units.value)) * 1e6);
index = 1;
} else if (e=="wavenumber") {
num_wl = (1 / (wavenumber.value * 1e2) * 1e6);
index = 2;
} else if (e=="wavelength") {
num_wl = wavelength.value;
index = 3;
}
let e_out = (consts.h * consts.c / (num_wl * 1e-6) * Number(1/energy_units.value)/consts.e);
let freq_out = (consts.c / (num_wl * 1e-6) * Number(1/freq_units.value));
let wn_out = (1 / (num_wl * 1e-6 * 1e2));
let wl_out = num_wl;
let val_array = [e_out,freq_out,wn_out,wl_out];
val_array.splice(index,1);
doc_array.splice(index,1);
for (let i=0;i<val_array.length;i++){
doc_array[i].value = val_array[i].toFixed(2);
}
};
yield div;
}