File tree Expand file tree Collapse file tree 1 file changed +10
-21
lines changed Expand file tree Collapse file tree 1 file changed +10
-21
lines changed Original file line number Diff line number Diff line change @@ -2,27 +2,16 @@ import { Fraction } from "../fraction-addition-and-subtraction/Fraction.ts";
2
2
import { fractionAdd } from "../fraction-addition-and-subtraction/index.ts" ;
3
3
4
4
export default function fraction ( cont : number [ ] ) : number [ ] {
5
- if ( cont . length === 1 ) {
6
- return [ cont [ 0 ] , 1 ] ;
7
- }
8
- if ( cont . length >= 2 ) {
9
- const last = fraction ( cont . slice ( - 1 ) ) ;
10
- let result = new Fraction ( { molecular : last [ 0 ] , denominator : last [ 1 ] } ) ;
11
- for ( let i = cont . length - 2 ; i >= 0 ; i -- ) {
12
- result = fractionAdd (
13
- [
14
- new Fraction ( { molecular : cont [ i ] , denominator : 1 } ) ,
15
- fractionReciprocal (
16
- result ,
17
- ) ,
18
- ] ,
19
- ) ;
20
- }
21
-
22
- return [ result . molecular , result . denominator ] ;
23
- }
24
-
25
- return [ 1 , 0 ] ;
5
+ const result = cont . reduceRight ( ( p , c ) =>
6
+ fractionAdd (
7
+ [
8
+ new Fraction ( { molecular : c , denominator : 1 } ) ,
9
+ fractionReciprocal (
10
+ p ,
11
+ ) ,
12
+ ] ,
13
+ ) , new Fraction ( { molecular : 1 , denominator : 0 } ) ) ;
14
+ return [ result . molecular , result . denominator ] ;
26
15
}
27
16
export function fractionReciprocal ( fraction : Fraction ) : Fraction {
28
17
return new Fraction ( {
You can’t perform that action at this time.
0 commit comments