← Back to challenges

Fractions Addition And Subtraction

JavaScriptHardalgorithmsnumbersregexstrings

Instructions

The function is given a string representing an expression of many fractions addition and subtraction. Compute the arithmetic result and return the string in the form of an irreducible fraction. Even if the result is an integer, e.g. 3, return it as 3/1.

Examples

fractionsAddition("4/5-6/4") ➞ "-7/10"

fractionsAddition("5/4-6/6") ➞ "1/4"

fractionsAddition("-1/2+1/2") ➞ "0/1"

fractionsAddition("-1/2+1/2+1/3") ➞ "1/3"

fractionsAddition("1/3-1/2") ➞ "-1/6"

fractionsAddition("5/3+1/3") ➞ "2/1"

Notes

N/A

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.