○ Power · accumulating over time
Coulomb counting
Track a battery's state of charge by integrating current over time, clamped to 0–100 %.
- Level 3 · Innovator
- 5 tests
- technically checked
Your task
A battery-management system estimates state of charge (SoC) by counting charge in and out. Each step, the charge moved is current × time.
Write stateOfCharge(startPct, capacityMah, currentsMa, dtS) returning the SoC (%) after each step:
- positive current means discharging, negative means charging;
- change in % = current × (dtS / 3600) / capacityMah × 100, subtracted from the SoC;
- clamp the SoC to the range 0–100 after every step.
Hints (0 of 3 shown)
What gets checked
- stateOfCharge(100, 1000, [500, 500], 3600) → [50, 0]
- stateOfCharge(50, 2000, [-1000], 1800) → [75]
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.