@@ -3,23 +3,24 @@ import { deduplication } from "./deduplication.ts";
3
3
import { Fraction } from "./Fraction.ts" ;
4
4
import { parseFraction } from "./parseFraction.ts" ;
5
5
import { FractionToString } from "./Fraction.ts" ;
6
-
7
- function fractionAddition ( expression : string ) : string {
8
- const fractions = parseFraction ( expression ) ;
9
-
6
+ export function fractionAdd ( fractions : Fraction [ ] ) : Fraction {
10
7
const denominator = deduplication (
11
8
fractions . map ( ( f ) => f . denominator ) ,
12
9
) . reduce ( ( a , b ) => a * b ) ;
13
10
const molecular = fractions
14
11
. map ( ( f ) => ( f . sign * f . molecular * denominator ) / f . denominator )
15
12
. reduce ( ( a , b ) => a + b ) ;
16
13
const gcd = greatestCommonDivisor ( molecular , denominator ) ;
14
+ return new Fraction ( {
15
+ denominator : denominator / gcd ,
16
+ molecular : molecular / gcd ,
17
+ } ) ;
18
+ }
19
+ function fractionAddition ( expression : string ) : string {
20
+ const fractions = parseFraction ( expression ) ;
17
21
18
22
return FractionToString (
19
- new Fraction ( {
20
- denominator : denominator / gcd ,
21
- molecular : molecular / gcd ,
22
- } ) ,
23
+ fractionAdd ( fractions ) ,
23
24
) ;
24
25
}
25
26
export default fractionAddition ;
0 commit comments