@@ -34,11 +34,7 @@ export function compareStrings(a: string, b: string): number {
34
34
}
35
35
36
36
export function computeLexicographicDistance ( a : string , b : string ) : number {
37
- //I literally used export function again because typescript kept telling me
38
- // that my return statements needed to be inside the body of a function.
39
- // I'm a bit skeptical because we are supposed to be DRY! and the only thing I
40
- // changed was the function name. But it worked. I'm just leaving this note
41
- // to remind myself to ask for clarification about this.
37
+
42
38
if ( a < b ) {
43
39
return - 1 ;
44
40
} else if ( a > b ) {
@@ -51,14 +47,7 @@ export function computeLexicographicDistance(a: string, b: string): number {
51
47
const result = compareStrings ( "Kimberlee" , "haldane" ) ;
52
48
console . log ( result ) ;
53
49
54
- //If I am understanding question 2 correctly, because the first function
55
- // 'compareStrings' calls on a second function 'computeLexicographicDistance',
56
- // I needed to set up both functions. The first function calls the second
57
- // and the second function does the actual calculation and determines if each string
58
- // is one of this set (-1, 1, 0). Testing is when the first function comes
59
- //back into play, comparing two strings and returning a numerical result. Sorry
60
- // for the long note, it took me a VERY long time to get to this understanding.
61
- // I just hope I am right.
50
+
62
51
63
52
/** (Q3)
64
53
* Converts a GPA on the 4.0 scale to the corresponding letter grade using the college board
@@ -74,7 +63,6 @@ export function myGrade(grade: number): string {
74
63
return gpa ;
75
64
}
76
65
77
- //I followed the same logic as question 2.
78
66
79
67
export function convertGpaToLetterGrade ( gpa : number ) : string {
80
68
if ( gpa >= 4.0 ) {
@@ -116,12 +104,12 @@ console.log(grade);
116
104
export function computeFactorial ( n : number ) : number {
117
105
let result = 1 ;
118
106
for ( let i = 1 ; i <= n ; i ++ ) {
119
- // I used i+1 and fell into an infinite loop. My bad!
107
+
120
108
result *= i ;
121
109
}
122
110
return result ;
123
111
}
124
- // Steps I followed: Describe the function, Declare the function, crawl inside the function
112
+
125
113
const n = 7 ;
126
114
console . log ( computeFactorial ( n ) ) ;
127
115
@@ -149,26 +137,18 @@ console.log(sum);
149
137
* @param n The first `n` of Fibonacci values to compute.
150
138
* @return An array containing the first `n` Fibonacci values.
151
139
*/
152
- //This question nearly had me in tears and I'm sad to say that in the end
153
- // I had to seek help. However, I think if I can explain what I learned as
154
- // I go, that help would not have been in vain. Again, pardon the long notes.
140
+
155
141
export function getFirstNFibonacciNumbers ( n : number ) : number [ ] {
156
- const myArray : number [ ] = [ ] ; // First: initiatialize an empty array inside the function
157
- // Next:Take care of base case since the first two Fibonacci numbers are 1, 1.
158
- // If n is 1, return an array with the first number. If n is 2, return an array with
159
- // the first two numbers. This step is crucial for ensuring the loop works properly.
142
+ const myArray : number [ ] = [ ] ;
160
143
if ( n <= 0 ) return [ ] ;
161
144
if ( n === 1 ) return [ 1 ] ;
162
145
if ( n === 2 ) return [ 1 , 1 ] ;
163
146
myArray . push ( 1 , 1 ) ;
164
147
165
148
for ( let i = 2 ; i < n ; i ++ ) {
166
- //initialization(starts at 2 because I added the first two numbers in the sequence above)
167
- // so this tells the function to start computing from the 3rd number. Condition: this tells
168
- // the function that it can only work with numbers less than the given number.
169
- // Increment: this tells the function to move on to the next number by 1 (no infinite loops).
149
+
170
150
const nextNum = myArray [ i - 1 ] + myArray [ i - 2 ] ;
171
- myArray . push ( nextNum ) ; // adds the new number to the array to continue the loop
151
+ myArray . push ( nextNum ) ;
172
152
}
173
153
return myArray ;
174
154
}
@@ -191,11 +171,11 @@ export function binarySearch(
191
171
value : number ,
192
172
) : number {
193
173
if ( end < start ) {
194
- // The range is not valid so just return -1.
174
+
195
175
return - 1 ;
196
176
}
197
177
198
- const pivotIndex = Math . floor ( ( start + end ) / 2 ) ; // The index in the middle of the array.
178
+ const pivotIndex = Math . floor ( ( start + end ) / 2 ) ;
199
179
200
180
if ( values [ pivotIndex ] === value ) {
201
181
return pivotIndex ;
@@ -204,7 +184,7 @@ export function binarySearch(
204
184
}
205
185
return binarySearch ( values , pivotIndex + 1 , end , value ) ;
206
186
}
207
- // This problem was much easier to write out because of question 6.
187
+
208
188
const values = [ 2 , 4 , 6 , 8 , 10 , 12 , 14 , 16 ] ;
209
189
const index = binarySearch ( values , 4 , 8 , 14 ) ;
210
190
console . log ( index ) ;
0 commit comments