@@ -10,21 +10,33 @@ import { computeLexicographicDistance } from "./util.js";
10
10
export function compareStrings ( a : string , b : string ) : number {
11
11
// The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
12
12
// if it is greater, and 0 if the strings are equal.
13
- const distance = computeLexicographicDistance ( a , b ) ;
13
+ const distance = computeLexicographicDistance ( a , b ) ;
14
14
15
- // TODO(you): Finish this method.
16
-
17
- return 0 ;
15
+ if ( distance < 0 ) {
16
+ return - 1 ;
17
+ } else if ( distance > 0 ) {
18
+ return 1 ;
19
+ } else {
20
+ return 0 ;
21
+ }
18
22
}
19
-
20
23
/**
21
24
* Computes the factorial of the given value of `n`.
22
25
*
23
26
* @param n The value for which to compute the factorial.
24
27
* @return The factorial of n.
25
28
*/
26
29
export function computeFactorial ( n : number ) : number {
27
- return 0 ;
30
+ if ( n === 0 ) {
31
+ return 1 ; // Base case: 0! and 1! both equal 1
32
+ }
33
+ else if ( n === 1 ) {
34
+ return 1 ;
35
+ }
36
+ else if ( n < 0 ) {
37
+ return 0 ;
38
+ }
39
+ return n * computeFactorial ( n - 1 ) ;
28
40
}
29
41
30
42
/**
@@ -34,6 +46,22 @@ export function computeFactorial(n: number): number {
34
46
* @return An array containing the first `n` Fibonacci values.
35
47
*/
36
48
export function getFirstNFibonacciNumbers ( n : number ) : number [ ] {
49
+ for ( let i = 0 ; i < n ; i ++ ) {
50
+ if ( n === 0 ) {
51
+ return [ ] ;
52
+ }
53
+ else if ( n === 1 ) {
54
+ return [ 1 ] ;
55
+ }
56
+ else if ( n === 2 ) {
57
+ return [ 1 , 1 ] ;
58
+ }
59
+ else {
60
+ const fib = getFirstNFibonacciNumbers ( n - 1 ) ;
61
+ fib . push ( fib [ fib . length - 1 ] + fib [ fib . length - 2 ] ) ;
62
+ return fib ;
63
+ }
64
+ }
37
65
return [ ] ;
38
66
}
39
67
@@ -57,9 +85,19 @@ export function binarySearch(
57
85
return - 1 ;
58
86
}
59
87
60
- const pivotIndex = Math . floor ( ( start + end ) / 2 ) ; // The index in the middle of the array.
61
88
62
- // TODO(you): Finish implementing this algorithm
89
+ const pivotIndex = Math . floor ( ( start + end ) / 2 ) ; // The index in the middle of the array.
90
+ if ( values [ pivotIndex ] === value ) {
91
+ return pivotIndex ;
92
+ } else if ( values [ pivotIndex ] > value ) {
93
+ return binarySearch ( values , start , pivotIndex - 1 , value ) ;
94
+ } else {
95
+ return binarySearch ( values , pivotIndex + 1 , end , value ) ;
96
+ return - 1 ;
97
+ }
98
+
99
+
100
+
63
101
64
102
// If values[pivotIndex] is equal to value then return `pivotIndex`.
65
103
// Else if values[pivotIndex] is greater than the value, then
0 commit comments