@@ -7,16 +7,15 @@ import { computeLexicographicDistance } from "./util.js";
77 * @param b The second `string` to compare.
88 * @return -1 if a is less than b, 1 if a is greater than b, and 0 otherwise.
99 */
10- export function compareStrings ( a : string , b : string ) : number {
11- const distance = computeLexicographicDistance ( a , b ) ;
12-
13- if ( distance < 0 ) {
10+ function compareStrings ( a : string , b : string ) : number {
11+ if ( a < b ) {
1412 return - 1 ;
15- } else if ( distance > 0 ) {
13+ } else if ( a > b ) {
1614 return 1 ;
1715 } else {
1816 return 0 ;
1917 }
18+
2019}
2120
2221/**
@@ -25,7 +24,7 @@ export function compareStrings(a: string, b: string): number {
2524 * @param n The value for which to compute the factorial.
2625 * @return The factorial of n.
2726 */
28- export function computeFactorial ( n : number ) : number {
27+ function computeFactorial ( n : number ) : number {
2928 if ( n === 0 ) {
3029 return 1 ;
3130 }
@@ -44,21 +43,17 @@ export function computeFactorial(n: number): number {
4443 * @param n The first `n` of Fibonacci values to compute.
4544 * @return An array containing the first `n` Fibonacci values.
4645 */
47- export function getFirstNFibonacciNumbers ( n : number ) : number [ ] {
48- if ( n <= 0 ) return [ ] ;
49-
50- const fib : number [ ] = [ 1 ] ;
51-
52- if ( n === 1 ) return fib ;
46+ function getFirstNFibonacciNumbers ( n : number ) : number [ ] {
47+ let fib : number [ ] = [ ] ;
5348
54- fib . push ( 1 ) ; // Add the second 1
55-
56- for ( let i = 2 ; i < n ; i ++ ) {
57- const next = fib [ i - 1 ] + fib [ i - 2 ] ;
58- fib . push ( next ) ;
49+ for ( let i = 0 ; i < n ; i ++ ) {
50+ if ( i === 0 || i === 1 ) {
51+ fib . push ( 1 ) ;
52+ } else {
53+ fib . push ( fib [ i - 1 ] + fib [ i - 2 ] ) ;
54+ }
5955 }
60-
61- return fib ;
56+ return fib ;
6257}
6358
6459/**
@@ -70,23 +65,16 @@ export function getFirstNFibonacciNumbers(n: number): number[] {
7065 * @param value The value to look for.
7166 * @return The index of the value if found in the array and -1 otherwise.
7267 */
73- export function binarySearch (
68+ function findValue (
7469 values : number [ ] ,
7570 start : number ,
7671 end : number ,
77- value : number ,
72+ value : number
7873) : number {
79- if ( end < start ) {
80- return - 1 ; // Base case: value not found
81- }
82-
83- const pivotIndex = Math . floor ( ( start + end ) / 2 ) ;
84-
85- if ( values [ pivotIndex ] === value ) {
86- return pivotIndex ;
87- } else if ( values [ pivotIndex ] > value ) {
88- return binarySearch ( values , start , pivotIndex - 1 , value ) ;
89- } else {
90- return binarySearch ( values , pivotIndex + 1 , end , value ) ;
74+ for ( let i + start ; i <= end ; i ++ ) {
75+ if ( values [ i ] === value ) {
76+ return i ;
77+ }
9178 }
79+ return - 1 ;
9280}
0 commit comments