@@ -43,7 +43,7 @@ export function compareStrings(a: string, b: string): number {
4343 * @return The letter grade ("A+", "A", "A-", "B+", etc.).
4444 */
4545export function convertGpaToLetterGrade ( gpa : number ) : string {
46- if ( gpa == 4.0 ) {
46+ if ( gpa === 4.0 ) {
4747 return "A" ;
4848 } else if ( gpa > 4.0 ) {
4949 return "A" ;
@@ -78,7 +78,7 @@ export function convertGpaToLetterGrade(gpa: number): string {
7878 */
7979export function computeFactorial ( n : number ) : number {
8080 let product = 1 ;
81- for ( let i = 1 ; 1 <= n ; i ++ ) {
81+ for ( let i = 1 ; i <= n ; i ++ ) {
8282 product *= i ;
8383 }
8484
@@ -107,7 +107,21 @@ export function addNumbers(values: number[]): number {
107107 * @return An array containing the first `n` Fibonacci values.
108108 */
109109export function getFirstNFibonacciNumbers ( n : number ) : number [ ] {
110- return [ ] ;
110+ let current = 1 ;
111+ let prev = 0 ;
112+
113+ const numbers = [ ] ;
114+
115+ for ( let i = 1 ; i <= n ; i ++ ) {
116+ numbers . push ( current ) ;
117+ const nextNum = current + prev ; // 1
118+ prev = current ; // 1
119+ current = nextNum
120+ }
121+
122+
123+
124+ return numbers ;
111125}
112126
113127/**
@@ -126,12 +140,22 @@ export function binarySearch(
126140 value : number ,
127141) : number {
128142 if ( end < start ) {
143+
144+
129145 // The range is not valid so just return -1.
130146 return - 1 ;
131147 }
132148
133149 const pivotIndex = Math . floor ( ( start + end ) / 2 ) ; // The index in the middle of the array.
134-
150+ if ( values [ pivotIndex ] === value ) {
151+ return pivotIndex ;
152+ }
153+ else if ( values [ pivotIndex ] > value ) {
154+ return binarySearch ( values , start , pivotIndex - 1 , value ) ;
155+ }
156+ else {
157+ return binarySearch ( values , pivotIndex + 1 , end , value ) ;
158+ }
135159 // TODO(you): Finish implementing this algorithm
136160
137161 // If values[pivotIndex] is equal to value then return `pivotIndex`.
0 commit comments