@@ -7,6 +7,9 @@ import { computeLexicographicDistance } from "./util.js";
7
7
* @return True if the age corresponds to a voting age and false otherwise.
8
8
*/
9
9
export function canVote ( age : number ) : boolean {
10
+ if ( age >= 18 ) {
11
+ return true ;
12
+ }
10
13
return false ;
11
14
}
12
15
@@ -15,16 +18,22 @@ export function canVote(age: number): boolean {
15
18
*
16
19
* @param a The first `string` to compare.
17
20
* @param b The second `string` to compare.
18
- * @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.
19
22
*/
20
23
export function compareStrings ( a : string , b : string ) : number {
21
- // The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
22
- // if it is greater, and 0 if the strings are equal.
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.
23
26
const distance = computeLexicographicDistance ( a , b ) ;
24
27
25
- // TODO(you): Finish this method.
28
+ if ( distance < 0 ) {
29
+ return - 1 ;
30
+ } else if ( distance > 0 ) {
31
+ return 1 ;
32
+ } else {
33
+ return 0 ;
34
+ }
26
35
27
- return 0 ;
36
+ // TODO(you): Finish this method.
28
37
}
29
38
30
39
/**
@@ -37,7 +46,31 @@ export function compareStrings(a: string, b: string): number {
37
46
* @return The letter grade ("A+", "A", "A-", "B+", etc.).
38
47
*/
39
48
export function convertGpaToLetterGrade ( gpa : number ) : string {
40
- 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
+ }
41
74
}
42
75
43
76
/**
@@ -47,7 +80,12 @@ export function convertGpaToLetterGrade(gpa: number): string {
47
80
* @return The factorial of n.
48
81
*/
49
82
export function computeFactorial ( n : number ) : number {
50
- return 0 ;
83
+ let product = 1 ;
84
+ for ( let i = 1 ; 1 <= n ; i ++ ) {
85
+ product *= i ;
86
+ }
87
+
88
+ return product ;
51
89
}
52
90
53
91
/**
@@ -57,7 +95,11 @@ export function computeFactorial(n: number): number {
57
95
* @return The sum of all the values.
58
96
*/
59
97
export function addNumbers ( values : number [ ] ) : number {
60
- return 0 ;
98
+ let sum = 0 ;
99
+ for ( const value of values ) {
100
+ sum += value ;
101
+ }
102
+ return sum ;
61
103
}
62
104
63
105
/**
@@ -67,7 +109,18 @@ export function addNumbers(values: number[]): number {
67
109
* @return An array containing the first `n` Fibonacci values.
68
110
*/
69
111
export function getFirstNFibonacciNumbers ( n : number ) : number [ ] {
70
- 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 ;
71
124
}
72
125
73
126
/**
@@ -91,12 +144,20 @@ export function binarySearch(
91
144
}
92
145
93
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
+ }
94
154
95
155
// TODO(you): Finish implementing this algorithm
96
156
97
157
// If values[pivotIndex] is equal to value then return `pivotIndex`.
98
158
// Else if values[pivotIndex] is greater than the value, then
99
159
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
100
160
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
161
+
101
162
return - 1 ;
102
163
}
0 commit comments