◇ Electronics · functions
Ohm's law calculator
Turn volts and ohms into amps and watts, then print power with sensible units.
- Level 1 · Explorer
- 7 tests
- technically checked
Your task
Before choosing a resistor you need to know the current through it and the power it must dissipate.
Write three functions:
- current(volts, ohms) returns amps, using I = V / R.
- power(volts, ohms) returns watts, using P = V² / R.
- withUnit(watts) returns a label: below 1 W show whole milliwatts (
Math.round(watts * 1000) + " mW"); from 1 W upwards show watts to two decimals (watts.toFixed(2) + " W").
A common through-hole resistor is rated ¼ W, so withUnit helps you spot a resistor that would overheat.
Hints (0 of 3 shown)
What gets checked
- current(5, 220) → ≈ 0.0227273 (± 0.000001)
- power(5, 220) → ≈ 0.1136364 (± 0.000001)
- withUnit(0.1136) → "114 mW"
- withUnit(1.5) → "1.50 W"
Plus 3 hidden tests for edge cases. They run in the sandbox like the others; only the result is shown.
Tab indents · Esc then Tab leaves the editor · Ctrl/⌘ Enter runs · Ctrl/⌘ Shift Enter checks · Ctrl/⌘ / comments
Press Run to see what your program prints.
Your code runs only in this browser, inside an isolated sandbox with no network access, and stops after 3 s. Nothing you type is sent to Tactiverge; drafts and progress are saved in this browser only.