@@ -10,21 +10,33 @@ import { computeLexicographicDistance } from "./util.js";
1010export 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.
13- const distance = computeLexicographicDistance ( a , b ) ;
13+ const distance = computeLexicographicDistance ( a , b ) ;
1414
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+ }
1822}
19-
2023/**
2124 * Computes the factorial of the given value of `n`.
2225 *
2326 * @param n The value for which to compute the factorial.
2427 * @return The factorial of n.
2528 */
2629export 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 ) ;
2840}
2941
3042/**
@@ -34,6 +46,22 @@ export function computeFactorial(n: number): number {
3446 * @return An array containing the first `n` Fibonacci values.
3547 */
3648export 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+ }
3765 return [ ] ;
3866}
3967
@@ -57,9 +85,19 @@ export function binarySearch(
5785 return - 1 ;
5886 }
5987
60- const pivotIndex = Math . floor ( ( start + end ) / 2 ) ; // The index in the middle of the array.
6188
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+
63101
64102 // If values[pivotIndex] is equal to value then return `pivotIndex`.
65103 // Else if values[pivotIndex] is greater than the value, then
0 commit comments