1+ import { error } from "console" ;
12import { computeLexicographicDistance } from "./util.js" ;
23
34/**
@@ -11,9 +12,8 @@ export function compareStrings(a: string, b: string): number {
1112 // The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
1213 // if it is greater, and 0 if the strings are equal.
1314 const distance = computeLexicographicDistance ( a , b ) ;
14-
15- // TODO(you): Finish this method.
16-
15+ if ( distance < 0 ) return - 1 ;
16+ if ( distance > 0 ) return 1 ;
1717 return 0 ;
1818}
1919
@@ -24,7 +24,17 @@ export function compareStrings(a: string, b: string): number {
2424 * @return The factorial of n.
2525 */
2626export function computeFactorial ( n : number ) : number {
27- return 0 ;
27+ if ( n < 0 ) {
28+ return 0 ;
29+ }
30+ if ( n === 0 || n === 1 ) {
31+ return 1 ;
32+ }
33+ let result = 1 ;
34+ for ( let i = 2 ; i <= n ; i ++ ) {
35+ result *= i ;
36+ }
37+ return result ;
2838}
2939
3040/**
@@ -34,7 +44,14 @@ export function computeFactorial(n: number): number {
3444 * @return An array containing the first `n` Fibonacci values.
3545 */
3646export function getFirstNFibonacciNumbers ( n : number ) : number [ ] {
37- return [ ] ;
47+ if ( n <= 0 ) return [ ] ;
48+ if ( n === 1 ) return [ 0 ] ;
49+
50+ const fibSeries : number [ ] = [ 1 , 1 ] ;
51+ for ( let i = 2 ; i < n ; i ++ ) {
52+ fibSeries . push ( fibSeries [ i - 1 ] + fibSeries [ i - 2 ] ) ;
53+ }
54+ return fibSeries ;
3855}
3956
4057/**
@@ -58,12 +75,16 @@ export function binarySearch(
5875 }
5976
6077 const pivotIndex = Math . floor ( ( start + end ) / 2 ) ; // The index in the middle of the array.
61-
62- // TODO(you): Finish implementing this algorithm
63-
64- // If values[pivotIndex] is equal to value then return `pivotIndex`.
65- // Else if values[pivotIndex] is greater than the value, then
66- // call ` binarySearch(values, start, pivotIndex - 1, value)` and return its value;
67- // Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
78+ if ( values [ pivotIndex ] === value ) {
79+ return pivotIndex ;
80+ } else if ( values [ pivotIndex ] > value ) {
81+ return binarySearch ( values , start , pivotIndex - 1 , value ) ;
82+ } else {
83+ return binarySearch ( values , pivotIndex + 1 , end , value ) ;
84+ }
6885 return - 1 ;
6986}
87+ // If values[pivotIndex] is equal to value then return `pivotIndex`.
88+ // Else if values[pivotIndex] is greater than the value, then
89+ // call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
90+ // Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
0 commit comments