@@ -11,9 +11,11 @@ export function compareStrings(a: string, b: string): number {
1111 // The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
1212 // if it is greater, and 0 if the strings are equal.
1313 const distance = computeLexicographicDistance ( a , b ) ;
14-
15- // TODO(you): Finish this method.
16-
14+ if ( distance < 0 ) {
15+ return - 1 ;
16+ } else if ( distance > 0 ) {
17+ return 1 ;
18+ }
1719 return 0 ;
1820}
1921
@@ -24,7 +26,19 @@ export function compareStrings(a: string, b: string): number {
2426 * @return The factorial of n.
2527 */
2628export function computeFactorial ( n : number ) : number {
27- return 0 ;
29+ if ( n < 0 ) {
30+ return 0 ;
31+ }
32+
33+ if ( n === 0 ) {
34+ return 1 ;
35+ }
36+
37+ let result = 1 ;
38+ for ( let i = 1 ; i <= n ; i ++ ) {
39+ result *= i ;
40+ }
41+ return result ;
2842}
2943
3044/**
@@ -34,7 +48,20 @@ export function computeFactorial(n: number): number {
3448 * @return An array containing the first `n` Fibonacci values.
3549 */
3650export function getFirstNFibonacciNumbers ( n : number ) : number [ ] {
37- return [ ] ;
51+ if ( n <= 0 ) {
52+ return [ ] ; // Return an empty array for non-positive lengths
53+ } else if ( n === 1 ) {
54+ return [ 1 ] ; // The first Fibonacci number is 1 (as per the request)
55+ }
56+
57+ const fibArray : number [ ] = [ 1 , 1 ] ; // Initialize with the first two Fibonacci numbers starting from 1
58+
59+ for ( let i = 2 ; i < n ; i ++ ) {
60+ const nextFib = fibArray [ i - 1 ] + fibArray [ i - 2 ] ;
61+ fibArray . push ( nextFib ) ;
62+ }
63+
64+ return fibArray ;
3865}
3966
4067/**
@@ -46,6 +73,7 @@ export function getFirstNFibonacciNumbers(n: number): number[] {
4673 * @param value The value to look for.
4774 * @return The index of the value if found in the array and -1 otherwise.
4875 */
76+
4977export function binarySearch (
5078 values : number [ ] ,
5179 start : number ,
@@ -57,8 +85,14 @@ export function binarySearch(
5785 return - 1 ;
5886 }
5987
60- const pivotIndex = Math . floor ( ( start + end ) / 2 ) ; // The index in the middle of the array.
61-
88+ const pivotIndex = Math . floor ( ( start + end ) / 2 ) ;
89+ if ( values [ pivotIndex ] == value ) {
90+ return pivotIndex ;
91+ } else if ( values [ pivotIndex ] > value ) {
92+ return binarySearch ( values , start , pivotIndex - 1 , value ) ;
93+ } else {
94+ return binarySearch ( values , pivotIndex + 1 , end , value ) ;
95+ }
6296 // TODO(you): Finish implementing this algorithm
6397
6498 // If values[pivotIndex] is equal to value then return `pivotIndex`.
0 commit comments