@@ -80,30 +80,30 @@ export function myGrade(grade: number): string {
80
80
// with this and see if I can't get it to look better (simpler/cleaner).
81
81
82
82
export function convertGpaToLetterGrade ( gpa : number ) : string {
83
- if ( gpa < 65 ) {
84
- return "F" ;
85
- } else if ( gpa < 67 && gpa >= 65 ) {
86
- return "D" ;
87
- } else if ( gpa < 70 && gpa >= 67 ) {
88
- return "D+" ;
89
- } else if ( gpa < 73 && gpa >= 70 ) {
90
- return "C-" ;
91
- } else if ( gpa < 77 && gpa >= 73 ) {
92
- return "C" ;
93
- } else if ( gpa < 80 && gpa >= 77 ) {
94
- return "C+" ;
95
- } else if ( gpa < 83 && gpa >= 80 ) {
96
- return "B-" ;
97
- } else if ( gpa < 87 && gpa >= 83 ) {
98
- return "B" ;
99
- } else if ( gpa < 90 && gpa >= 87 ) {
100
- return "B+" ;
101
- } else if ( gpa < 93 && gpa >= 90 ) {
102
- return "A-" ;
103
- } else if ( gpa < 97 && gpa >= 93 ) {
104
- return "A" ;
105
- } else if ( gpa <= 100 && gpa >= 97 ) {
83
+ if ( gpa >= 4.0 ) {
106
84
return "A+" ;
85
+ } else if ( gpa > 3.7 && gpa < 4.0 ) {
86
+ return "A" ;
87
+ } else if ( gpa > 3.3 && gpa <= 3.7 ) {
88
+ return "A-" ;
89
+ } else if ( gpa > 3.0 && gpa <= 3.3 ) {
90
+ return "B+" ;
91
+ } else if ( gpa > 2.7 && gpa <= 3.0 ) {
92
+ return "B" ;
93
+ } else if ( gpa > 2.3 && gpa <= 2.7 ) {
94
+ return "B-" ;
95
+ } else if ( gpa > 2.0 && gpa <= 2.3 ) {
96
+ return "C+" ;
97
+ } else if ( gpa > 1.7 && gpa <= 2.0 ) {
98
+ return "C" ;
99
+ } else if ( gpa > 1.3 && gpa <= 1.7 ) {
100
+ return "C-" ;
101
+ } else if ( gpa > 1.0 && gpa <= 1.3 ) {
102
+ return "D+" ;
103
+ } else if ( gpa > 0.0 && gpa <= 1.0 ) {
104
+ return "D" ;
105
+ } else if ( gpa < 0.0 ) {
106
+ return "F" ;
107
107
} else {
108
108
return "unable to assess" ;
109
109
}
@@ -120,7 +120,8 @@ console.log(grade);
120
120
*/
121
121
export function computeFactorial ( n : number ) : number {
122
122
let result = 1 ;
123
- for ( let i = 1 ; i <= n ; i ++ ) { // I used i+1 and fell into an infinite loop. My bad!
123
+ for ( let i = 1 ; i <= n ; i ++ ) {
124
+ // I used i+1 and fell into an infinite loop. My bad!
124
125
result *= i ;
125
126
}
126
127
return result ;
@@ -129,7 +130,6 @@ export function computeFactorial(n: number): number {
129
130
const n = 7 ;
130
131
console . log ( computeFactorial ( n ) ) ;
131
132
132
-
133
133
/** (Q5)
134
134
* Adds all of the provided values and returns the sum.
135
135
*
@@ -154,10 +154,32 @@ console.log(sum);
154
154
* @param n The first `n` of Fibonacci values to compute.
155
155
* @return An array containing the first `n` Fibonacci values.
156
156
*/
157
+ //This question nearly had me in tears and I'm sad to say that in the end
158
+ // I had to seek help. However, I think if I can explain what I learned as
159
+ // I go, that help would not have been in vain. Again, pardon the long notes.
157
160
export function getFirstNFibonacciNumbers ( n : number ) : number [ ] {
158
- return [ ] ;
161
+ const myArray : number [ ] = [ ] ; // First: initiatialize an empty array inside the function
162
+ // Next:Take care of base case since the first two Fibonacci numbers are 1, 1.
163
+ // If n is 1, return an array with the first number. If n is 2, return an array with
164
+ // the first two numbers. This step is crucial for ensuring the loop works properly.
165
+ if ( n <= 0 ) return [ ] ;
166
+ if ( n === 1 ) return [ 1 ] ;
167
+ if ( n === 2 ) return [ 1 , 1 ] ;
168
+ myArray . push ( 1 , 1 ) ;
169
+
170
+ for ( let i = 2 ; i < n ; i ++ ) {
171
+ //initialization(stats at 2 because I added the first two numbers in the sequence above)
172
+ // so this tells the function to start computing from the 3rd number. Condition: this tells
173
+ // the function that it can only work with numbers less than the given number.
174
+ // Increment: this tells the function to move on to the next number by 1 (no infinite loops).
175
+ const nextNum = myArray [ i - 1 ] + myArray [ i - 2 ] ;
176
+ myArray . push ( nextNum ) ; // adds the new number to the array to continue the loop
177
+ }
178
+ return myArray ;
159
179
}
160
180
181
+ console . log ( getFirstNFibonacciNumbers ( 7 ) ) ;
182
+
161
183
/** (Q7)
162
184
* Finds a value in an array of values.
163
185
*
@@ -180,11 +202,11 @@ export function binarySearch(
180
202
181
203
const pivotIndex = Math . floor ( ( start + end ) / 2 ) ; // The index in the middle of the array.
182
204
183
- // TODO(you): Finish implementing this algorithm
184
-
185
- // If values[pivotIndex] is equal to value then return `pivotIndex`.
186
- // Else if values[pivotIndex] is greater than the value, then
187
- // call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
188
- // Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
189
- return - 1 ;
205
+ if ( values [ pivotIndex ] === value ) {
206
+ return pivotIndex ;
207
+ } else if ( values [ pivotIndex ] > value ) {
208
+ return binarySearch ( values , start , pivotIndex - 1 , value ) ;
209
+ }
210
+ return binarySearch ( values , pivotIndex + 1 , end , value ) ;
190
211
}
212
+ // This problem was much easier to write out because of question 6.
0 commit comments