Calculate the Missing Value with Ohm's Law

Published by Andreas Skauen Bertelsen in

Create a function that calculates the missing value of 3 inputs using Ohm's law. The inputs are u, r or i (aka: voltage, resistance and current).

Ohm's law:

U = R * I

Return the missing value rounded to two decimal places.

Examples

ohms_law(12, 220, nil) ➞ 0.05

ohms_law(230, nil, 2) ➞ 115

ohms_law(nil, 220, 0.02) ➞ 4.4

ohms_law(nil, nil, 10) ➞ "Invalid"

ohms_law(500, 50, 10) ➞ "Invalid"

Notes

  • Missing values will be nil
  • If there is more than one missing value, or no missing value, return "Invalid"
  • Only numbers will be given.
Watch a quick demo on how Edabit works.