@@ -13,8 +13,13 @@ export function compareStrings(a: string, b: string): number {
1313 const distance = computeLexicographicDistance ( a , b ) ;
1414
1515 // TODO(you): Finish this method.
16-
16+ if ( distance < 0 ) {
17+ return - 1 ;
18+ } else if ( distance > 0 ) {
19+ return 1 ;
20+ } else {
1721 return 0 ;
22+ }
1823}
1924
2025/**
@@ -24,7 +29,19 @@ export function compareStrings(a: string, b: string): number {
2429 * @return The factorial of n.
2530 */
2631export function computeFactorial ( n : number ) : number {
27- return 0 ;
32+ if ( n === 0 ) {
33+ return 1 ; // The factorial of 0 is 1.
34+ }
35+
36+ if ( n < 0 ) {
37+ return 0 ; // Factorial is not defined for negative numbers.
38+ }
39+
40+ let result = 1 ;
41+ for ( let i = 1 ; i <= n ; i ++ ) {
42+ result *= i ; // Multiply result by i for each i from 1 to n.
43+ }
44+ return result ;
2845}
2946
3047/**
@@ -34,7 +51,28 @@ export function computeFactorial(n: number): number {
3451 * @return An array containing the first `n` Fibonacci values.
3552 */
3653export function getFirstNFibonacciNumbers ( n : number ) : number [ ] {
37- return [ ] ;
54+
55+
56+ if ( n === 0 ) {
57+ return [ ] ; // If n is 0, return an empty array.
58+ }
59+
60+ const fibonacciNumbers : number [ ] = [ 1 ] ; // Initialize the array with the first Fibonacci number.
61+
62+
63+ if ( n === 1 ) {
64+ return fibonacciNumbers ; // If n is 1, return the array with the first Fibonacci number.
65+ }
66+
67+ fibonacciNumbers . push ( 1 ) ; // Add the second Fibonacci number (1) to the array.
68+
69+
70+ for ( let i = 2 ; i < n ; i ++ ) {
71+ const nextFibonacciNumber =
72+ fibonacciNumbers [ fibonacciNumbers . length - 1 ] + fibonacciNumbers [ fibonacciNumbers . length - 2 ] ; // The next Fibonacci number is the sum of the last two.
73+ fibonacciNumbers . push ( nextFibonacciNumber ) ; // Add the next Fibonacci number
74+ }
75+ return fibonacciNumbers ; // Return the array of Fibonacci numbers.
3876}
3977
4078/**
@@ -60,10 +98,27 @@ export function binarySearch(
6098 const pivotIndex = Math . floor ( ( start + end ) / 2 ) ; // The index in the middle of the array.
6199
62100 // TODO(you): Finish implementing this algorithm
101+ if ( values [ pivotIndex ] === value ) {
102+ // If the value at pivotIndex is equal to the value we are looking for, return pivotIndex.
103+ return pivotIndex ;
104+ }
105+ else if ( values [ pivotIndex ] > value ) {
106+ // If the value at pivotIndex is greater than the value we are looking for,
107+ // search the left half of the array.
108+ return binarySearch ( values , start , pivotIndex - 1 , value ) ;
109+ }
110+ else {
111+ // If the value at pivotIndex is less than the value we are looking for,
112+ // search the right half of the array.
113+ return binarySearch ( values , pivotIndex + 1 , end , value ) ;
114+ }
115+
116+
63117
64118 // If values[pivotIndex] is equal to value then return `pivotIndex`.
65119 // Else if values[pivotIndex] is greater than the value, then
66120 // call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
67121 // Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
68- return - 1 ;
122+
123+ return - 1 ;
69124}
0 commit comments