@@ -8,11 +8,10 @@ import { computeLexicographicDistance } from "./util.js";
8
8
* @return -1 if a is less than b, 1 if a is greater than b, and 0 otherwise.
9
9
*/
10
10
export function compareStrings ( a : string , b : string ) : number {
11
- // The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
12
- // if it is greater, and 0 if the strings are equal.
13
11
const distance = computeLexicographicDistance ( a , b ) ;
14
-
15
- // TODO(you): Finish this method.
12
+
13
+ if ( a < b ) return - 1 ;
14
+ if ( a > b ) return 1 ;
16
15
17
16
return 0 ;
18
17
}
@@ -24,19 +23,36 @@ export function compareStrings(a: string, b: string): number {
24
23
* @return The factorial of n.
25
24
*/
26
25
export function computeFactorial ( n : number ) : number {
27
- return 0 ;
26
+
27
+ if ( n < 0 ) {
28
+ console . error ( "Factorial is not defined for negative numbers." ) ;
29
+ return - 1 ;
30
+ }
31
+ if ( n === 0 || n === 1 ) return 1 ;
32
+ return n * computeFactorial ( n - 1 ) ;
28
33
}
29
34
35
+ console . log ( computeFactorial ( - 2 ) ) ;
36
+
37
+
38
+
30
39
/**
31
40
* Returns an array of the first `n` Fibonacci numbers starting from 1.
32
41
*
33
42
* @param n The first `n` of Fibonacci values to compute.
34
43
* @return An array containing the first `n` Fibonacci values.
35
44
*/
36
45
export function getFirstNFibonacciNumbers ( n : number ) : number [ ] {
37
- return [ ] ;
46
+ if ( n <= 0 ) return [ ] ;
47
+ if ( n === 1 ) return [ 1 ] ;
48
+
49
+ const fib : number [ ] = [ 1 , 1 ] ;
50
+ for ( let i = 2 ; i < n ; i ++ ) {
51
+ fib . push ( fib [ i - 1 ] + fib [ i - 2 ] ) ;
52
+ }
53
+ return fib ;
38
54
}
39
-
55
+
40
56
/**
41
57
* Finds a value in an array of values.
42
58
*
@@ -46,24 +62,23 @@ export function getFirstNFibonacciNumbers(n: number): number[] {
46
62
* @param value The value to look for.
47
63
* @return The index of the value if found in the array and -1 otherwise.
48
64
*/
49
- export function binarySearch (
50
- values : number [ ] ,
51
- start : number ,
52
- end : number ,
53
- value : number ,
54
- ) : number {
55
- if ( end < start ) {
56
- // The range is not valid so just return -1.
57
- return - 1 ;
58
- }
65
+ export function binarySearch ( values : number [ ] , value : number ) : number {
66
+ let start = 0 ;
67
+ let end = values . length - 1 ;
59
68
60
- const pivotIndex = Math . floor ( ( start + end ) / 2 ) ; // The index in the middle of the array.
69
+ while ( start <= end ) {
70
+ let mid = Math . floor ( ( start + end ) / 2 ) ;
61
71
62
- // TODO(you): Finish implementing this algorithm
72
+ if ( values [ mid ] === value ) {
73
+ return mid ;
74
+ } else if ( values [ mid ] > value ) {
75
+ end = mid - 1 ;
76
+ } else {
77
+ start = mid + 1 ;
78
+ }
79
+ }
63
80
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.
68
- return - 1 ;
81
+ return - 1 ;
69
82
}
83
+
84
+
0 commit comments