@@ -18,25 +18,22 @@ export function canVote(age: number): boolean {
18
18
*
19
19
* @param a The first `string` to compare.
20
20
* @param b The second `string` to compare.
21
- * @return -1 if a is less than b, 1 if a is greater than b, and 0 otherwise.
21
+ * @return -1 if a (the first 'string) is less than b (the second 'string') , 1 if a is greater than b, and 0 otherwise.
22
22
*/
23
23
export function compareStrings ( a : string , b : string ) : number {
24
- if ( a > b ) {
24
+ // The distance will be a number less than 0 if string `a` is lexicographically less than `b`,
25
+ // 1 if it is greater b, and 0 if both strings are equal.
26
+ const distance = computeLexicographicDistance ( a , b ) ;
27
+
28
+ if ( distance < 0 ) {
29
+ return - 1 ;
30
+ } else if ( distance > 0 ) {
25
31
return 1 ;
26
- }
27
- if ( a < b ) {
28
- return - 1 ;
29
- }
30
- if ( a = b ) {
32
+ } else {
31
33
return 0 ;
32
34
}
33
- // The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
34
- // if it is greater, and 0 if the strings are equal.
35
- const distance = computeLexicographicDistance ( a , b ) ;
36
-
37
- // TODO(you): Finish this method.
38
35
39
- return 0 ;
36
+ // TODO(you): Finish this method.
40
37
}
41
38
42
39
/**
@@ -49,7 +46,31 @@ export function compareStrings(a: string, b: string): number {
49
46
* @return The letter grade ("A+", "A", "A-", "B+", etc.).
50
47
*/
51
48
export function convertGpaToLetterGrade ( gpa : number ) : string {
52
- return "F" ;
49
+ if ( gpa === 4.0 ) {
50
+ return "A" ;
51
+ } else if ( gpa > 4.0 ) {
52
+ return "A" ;
53
+ } else if ( gpa <= 3.99 && gpa >= 3.7 ) {
54
+ return "A-" ;
55
+ } else if ( gpa <= 3.69 && gpa >= 3.3 ) {
56
+ return "B+" ;
57
+ } else if ( gpa <= 3.29 && gpa >= 3.0 ) {
58
+ return "B" ;
59
+ } else if ( gpa <= 2.99 && gpa >= 2.7 ) {
60
+ return "B-" ;
61
+ } else if ( gpa <= 2.69 && gpa >= 2.3 ) {
62
+ return "C+" ;
63
+ } else if ( gpa <= 2.29 && gpa >= 2.0 ) {
64
+ return "C" ;
65
+ } else if ( gpa <= 1.99 && gpa >= 1.7 ) {
66
+ return "C-" ;
67
+ } else if ( gpa <= 1.69 && gpa >= 1.3 ) {
68
+ return "D+" ;
69
+ } else if ( gpa <= 1.29 && gpa >= 1.0 ) {
70
+ return "D" ;
71
+ } else {
72
+ return "F" ;
73
+ }
53
74
}
54
75
55
76
/**
@@ -59,9 +80,12 @@ export function convertGpaToLetterGrade(gpa: number): string {
59
80
* @return The factorial of n.
60
81
*/
61
82
export function computeFactorial ( n : number ) : number {
62
- let product = 1
63
- for ( )
64
- return 0 ;
83
+ let product = 1 ;
84
+ for ( let i = 1 ; 1 <= n ; i ++ ) {
85
+ product *= i ;
86
+ }
87
+
88
+ return product ;
65
89
}
66
90
67
91
/**
@@ -71,7 +95,11 @@ export function computeFactorial(n: number): number {
71
95
* @return The sum of all the values.
72
96
*/
73
97
export function addNumbers ( values : number [ ] ) : number {
74
- return 0 ;
98
+ let sum = 0 ;
99
+ for ( const value of values ) {
100
+ sum += value ;
101
+ }
102
+ return sum ;
75
103
}
76
104
77
105
/**
@@ -81,7 +109,18 @@ export function addNumbers(values: number[]): number {
81
109
* @return An array containing the first `n` Fibonacci values.
82
110
*/
83
111
export function getFirstNFibonacciNumbers ( n : number ) : number [ ] {
84
- return [ ] ;
112
+ let current = 1 ;
113
+ let prev = 0 ;
114
+
115
+ const numbers = [ ] ;
116
+ for ( let i = 1 ; i <= n ; i ++ ) {
117
+ numbers . push ( current ) ;
118
+ const nextNum = current + prev ;
119
+ prev = current ;
120
+ current = nextNum ;
121
+ }
122
+
123
+ return numbers ;
85
124
}
86
125
87
126
/**
@@ -105,12 +144,20 @@ export function binarySearch(
105
144
}
106
145
107
146
const pivotIndex = Math . floor ( ( start + end ) / 2 ) ; // The index in the middle of the array.
147
+ if ( values [ pivotIndex ] === value ) {
148
+ return pivotIndex ;
149
+ } else if ( values [ pivotIndex ] > value ) {
150
+ return binarySearch ( values , start , pivotIndex - 1 , value ) ;
151
+ } else {
152
+ return binarySearch ( values , pivotIndex + 1 , end , value ) ;
153
+ }
108
154
109
155
// TODO(you): Finish implementing this algorithm
110
156
111
157
// If values[pivotIndex] is equal to value then return `pivotIndex`.
112
158
// Else if values[pivotIndex] is greater than the value, then
113
159
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
114
160
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
161
+
115
162
return - 1 ;
116
163
}
0 commit comments