Calculate The Missing Value With Ohm's Law

Published by Deep Xavier in

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

Ohm's law:

V = R * I

Return the missing value rounded to two decimal places.

Examples

calculate(46.6, "", 54.4) ➞ 0.86

calculate(54.0, 46.0, "") ➞ 1.17

calculate(230.0, "", 2.0) ➞ 115.0

calculate("", 220.0, 0.02) ➞ 4.4

calculate("", "", 10.0) ➞ "Invalid"

calculate(500.0, 50.0, 10.0) ➞ "Invalid"

Notes

  • Missing values will be presented as an empty string ""
  • 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.