← Back to challenges

Evaluating Simple Algebra

JavaScriptHardstringsalgebramathnumbers

Instructions

Given a string containing an algebraic equation, calculate and return the value of x. You'll only be given equations for simple addition and subtraction.

Examples

evalAlgebra("2 + x = 19") ➞ 17

evalAlgebra("4 - x = 1") ➞ 3

evalAlgebra("23 + 1 = x") ➞ 24

Notes

  • There are spaces between every number and symbol in the string.
  • x may be a negative number.
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.