@@ -43,18 +43,25 @@ export function compareStrings(a: string, b: string): number {
43
43
* @return The letter grade ("A+", "A", "A-", "B+", etc.).
44
44
*/
45
45
export function convertGpaToLetterGrade ( gpa : number ) : string {
46
- if ( gpa > 4.0 ) return "A+" ;
47
- if ( gpa >= 3.7 ) return "A" ;
48
- if ( gpa >= 3.3 ) return "A-" ;
49
- if ( gpa >= 3.0 ) return "B+" ;
50
- if ( gpa >= 2.7 ) return "B" ;
51
- if ( gpa >= 2.3 ) return "B-" ;
52
- if ( gpa >= 2.0 ) return "C+" ;
53
- if ( gpa >= 1.7 ) return "C" ;
54
- if ( gpa >= 1.3 ) return "C-" ;
55
- if ( gpa >= 1.0 ) return "D+" ;
56
- if ( gpa >= 0.0 ) return "D" ;
57
-
46
+ if ( gpa >= 4.0 ) return "A" ;
47
+ if ( gpa >= 3.99 ) return "A-" ;
48
+ if ( gpa >= 3.7 ) return "A-" ;
49
+ if ( gpa >= 3.69 ) return "B+" ;
50
+ if ( gpa >= 3.3 ) return "B+" ;
51
+ if ( gpa >= 3.29 ) return "B" ;
52
+ if ( gpa >= 3.0 ) return "B" ;
53
+ if ( gpa >= 2.99 ) return "B-" ;
54
+ if ( gpa >= 2.7 ) return "B-" ;
55
+ if ( gpa >= 2.69 ) return "C+" ;
56
+ if ( gpa >= 2.3 ) return "C+" ;
57
+ if ( gpa >= 2.29 ) return "C" ;
58
+ if ( gpa >= 2.0 ) return "C" ;
59
+ if ( gpa >= 1.99 ) return "C-" ;
60
+ if ( gpa >= 1.7 ) return "C-" ;
61
+ if ( gpa >= 1.69 ) return "D+" ;
62
+ if ( gpa >= 1.3 ) return "D+" ;
63
+ if ( gpa >= 1.29 ) return "D" ;
64
+ if ( gpa >= 1.0 ) return "D" ;
58
65
return "F" ;
59
66
}
60
67
@@ -96,7 +103,23 @@ export function addNumbers(values: number[]): number {
96
103
* @return An array containing the first `n` Fibonacci values.
97
104
*/
98
105
export function getFirstNFibonacciNumbers ( n : number ) : number [ ] {
99
- return [ ] ;
106
+ const fibNumbers : number [ ] = [ ] ;
107
+
108
+ if ( n <= 0 ) return fibNumbers ;
109
+
110
+ let first = 1 ,
111
+ second = 1 ;
112
+ fibNumbers . push ( first ) ;
113
+ if ( n > 1 ) fibNumbers . push ( second ) ;
114
+
115
+ for ( let i = 2 ; i < n ; i ++ ) {
116
+ const next = first + second ;
117
+ fibNumbers . push ( next ) ;
118
+ first = second ;
119
+ second = next ;
120
+ }
121
+
122
+ return fibNumbers ;
100
123
}
101
124
102
125
/**
@@ -112,21 +135,41 @@ export function binarySearch(
112
135
values : number [ ] ,
113
136
start : number ,
114
137
end : number ,
115
- value : number ,
138
+ target : number ,
116
139
) : number {
117
- if ( end < start ) {
118
- // The range is not valid so just return -1.
119
-
140
+ if ( start > end ) {
120
141
return - 1 ;
121
142
}
122
143
144
+ const mid = Math . floor ( ( start + end ) / 2 ) ;
145
+
146
+ if ( values [ mid ] === target ) {
147
+ return mid ;
148
+ } else if ( values [ mid ] > target ) {
149
+ return binarySearch ( values , start , mid - 1 , target ) ;
150
+ } else {
151
+ return binarySearch ( values , mid + 1 , end , target ) ;
152
+ }
153
+
154
+ /**
155
+ * The following code is part of the binary search logic:
156
+ */
157
+
123
158
const pivotIndex = Math . floor ( ( start + end ) / 2 ) ; // The index in the middle of the array.
124
159
125
- // TODO(you): Finish implementing this algorithm
160
+ if ( values [ pivotIndex ] === target ) {
161
+ return pivotIndex ;
162
+ } else if ( values [ pivotIndex ] > target ) {
163
+ return binarySearch ( values , start , pivotIndex - 1 , target ) ;
164
+ } else {
165
+ return binarySearch ( values , pivotIndex + 1 , end , target ) ;
166
+ }
126
167
127
- // If values[pivotIndex] is equal to value then return `pivotIndex`.
128
- // Else if values[pivotIndex] is greater than the value, then
129
- // call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
130
- // Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
131
168
return - 1 ;
132
169
}
170
+ // TODO(you): Finish implementing this algorithm
171
+
172
+ // If values[pivotIndex] is equal to value then return `pivotIndex`.
173
+ // Else if values[pivotIndex] is greater than the value, then
174
+ // call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
175
+ // Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
0 commit comments