@@ -8,11 +8,10 @@ import { computeLexicographicDistance } from "./util.js";
88 * @return -1 if a is less than b, 1 if a is greater than b, and 0 otherwise.
99 */
1010export 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.
1311 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 ;
1615
1716 return 0 ;
1817}
@@ -24,19 +23,36 @@ export function compareStrings(a: string, b: string): number {
2423 * @return The factorial of n.
2524 */
2625export 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 ) ;
2833}
2934
35+ console . log ( computeFactorial ( - 2 ) ) ;
36+
37+
38+
3039/**
3140 * Returns an array of the first `n` Fibonacci numbers starting from 1.
3241 *
3342 * @param n The first `n` of Fibonacci values to compute.
3443 * @return An array containing the first `n` Fibonacci values.
3544 */
3645export 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 ;
3854}
39-
55+
4056/**
4157 * Finds a value in an array of values.
4258 *
@@ -46,24 +62,23 @@ export function getFirstNFibonacciNumbers(n: number): number[] {
4662 * @param value The value to look for.
4763 * @return The index of the value if found in the array and -1 otherwise.
4864 */
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 ;
5968
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 ) ;
6171
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+ }
6380
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 ;
6982}
83+
84+
0 commit comments