Ohm’s Law Calculator

Enter any two values to calculate the third using Ohm’s Law: V = I × R


V
mV


A
mA
µA


Ω


function calculateOhmsLaw() {
const V = parseFloat(document.getElementById(“voltage”).value);
const I = parseFloat(document.getElementById(“current”).value);
const R = parseFloat(document.getElementById(“resistance”).value);

const Vunit = parseFloat(document.getElementById(“voltageUnit”).value);
const Iunit = parseFloat(document.getElementById(“currentUnit”).value);
const Runit = parseFloat(document.getElementById(“resistanceUnit”).value);

const hasV = !isNaN(V);
const hasI = !isNaN(I);
const hasR = !isNaN(R);

let result = ”;

if ((hasV && hasI && hasR) || (!hasV && !hasI && !hasR)) {
result = “Please enter exactly two values to calculate the third.”;
} else if (!hasV && hasI && hasR) {
const voltage = (I * Iunit) * (R * Runit);
result = `Voltage = ${voltage.toFixed(3)} V`;
} else if (!hasI && hasV && hasR) {
const current = (V * Vunit) / (R * Runit);
let unit = “A”, val = current;
if (current < 1e-6) { val *= 1e6; unit = "µA"; }
else if (current = 1e6) { val /= 1e6; unit = “MΩ”; }
else if (resistance >= 1e3) { val /= 1e3; unit = “kΩ”; }
result = `Resistance = ${val.toFixed(3)} ${unit}`;
} else {
result = “Please enter exactly two values to compute the third.”;
}

document.getElementById(“ohmsResult”).innerText = result;
}

function resetOhms() {
document.getElementById(“ohmsResult”).innerText = “”;
}


(adsbygoogle = window.adsbygoogle || []).push({});