@@ -7,7 +7,11 @@ 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
+ } else {
10
13
return false ;
14
+ }
11
15
}
12
16
13
17
/**
@@ -21,12 +25,15 @@ export function compareStrings(a: string, b: string): number {
21
25
// The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
22
26
// if it is greater, and 0 if the strings are equal.
23
27
const distance = computeLexicographicDistance ( a , b ) ;
24
-
25
- // TODO(you): Finish this method.
26
-
27
- return 0 ;
28
+ if ( distance < 0 ) {
29
+ return - 1 ;
30
+ } else {
31
+ return distance
32
+ }
33
+
28
34
}
29
35
36
+
30
37
/**
31
38
* Converts a GPA on the 4.0 scale to the corresponding letter grade using the college board
32
39
* scale. See
@@ -37,17 +44,57 @@ export function compareStrings(a: string, b: string): number {
37
44
* @return The letter grade ("A+", "A", "A-", "B+", etc.).
38
45
*/
39
46
export function convertGpaToLetterGrade ( gpa : number ) : string {
40
- return "F" ;
47
+ if ( gpa >= 4.0 ) {
48
+ return "A" ;
49
+ }
50
+ else if ( gpa >= 3.7 && gpa <= 3.99 ) {
51
+ return "A-" ;
52
+ }
53
+ else if ( gpa >= 3.3 && gpa <= 3.69 ) {
54
+ return "B+" ;
55
+ }
56
+ else if ( gpa >= 3.0 && gpa <= 3.29 ) {
57
+ return "B" ;
58
+ }
59
+ else if ( gpa >= 2.7 && gpa <= 2.99 ) {
60
+ return "B-" ;
61
+ }
62
+ else if ( gpa >= 2.3 && gpa <= 2.69 ) {
63
+ return "C+" ;
64
+ }
65
+ else if ( gpa >= 2.0 && gpa <= 2.29 ) {
66
+ return "C" ;
67
+ }
68
+ else if ( gpa >= 1.7 && gpa <= 1.99 ) {
69
+ return "C-" ;
70
+ }
71
+ else if ( gpa >= 1.3 && gpa <= 1.69 ) {
72
+ return "D+" ;
73
+ }
74
+ else if ( gpa >= 1 && gpa <= 1.29 ) {
75
+ return "D" ;
76
+ }
77
+ else {
78
+ return "F" ;
79
+ }
41
80
}
42
81
82
+
43
83
/**
44
84
* Computes the factorial of the given value of `n`.
45
85
*
46
86
* @param n The value for which to compute the factorial.
47
87
* @return The factorial of n.
48
88
*/
49
89
export function computeFactorial ( n : number ) : number {
50
- return 0 ;
90
+ if ( n === 0 ) {
91
+ return 1 ;
92
+ }
93
+ let sum = 1 ;
94
+ for ( let i = 1 ; i <= n ; i ++ ) {
95
+ sum *= i ;
96
+ }
97
+ return sum ;
51
98
}
52
99
53
100
/**
@@ -57,7 +104,11 @@ export function computeFactorial(n: number): number {
57
104
* @return The sum of all the values.
58
105
*/
59
106
export function addNumbers ( values : number [ ] ) : number {
60
- return 0 ;
107
+ let sum = 0 ;
108
+ for ( const value of values ) {
109
+ sum += value ;
110
+ }
111
+ return sum ;
61
112
}
62
113
63
114
/**
@@ -67,9 +118,21 @@ export function addNumbers(values: number[]): number {
67
118
* @return An array containing the first `n` Fibonacci values.
68
119
*/
69
120
export function getFirstNFibonacciNumbers ( n : number ) : number [ ] {
70
- return [ ] ;
121
+ const num : number [ ] = [ ] ;
122
+ for ( let i = 0 ; i < n ; i ++ ) {
123
+ if ( i === 0 ) {
124
+ num [ i ] = 1 ;
125
+ } else if ( i === 1 ) {
126
+ num [ i ] = 1 ;
127
+ } else {
128
+ num [ i ] = num [ i - 1 ] + num [ i - 2 ] ;
129
+ }
130
+ }
131
+ return num ;
71
132
}
72
133
134
+
135
+
73
136
/**
74
137
* Finds a value in an array of values.
75
138
*
@@ -93,6 +156,14 @@ export function binarySearch(
93
156
const pivotIndex = Math . floor ( ( start + end ) / 2 ) ; // The index in the middle of the array.
94
157
95
158
// TODO(you): Finish implementing this algorithm
159
+ if ( values [ pivotIndex ] == value ) {
160
+ return pivotIndex ;
161
+ }
162
+ else if ( values [ pivotIndex ] > value ) {
163
+ return binarySearch ( values , start , pivotIndex - 1 , value ) ;
164
+ } else {
165
+ return binarySearch ( values , pivotIndex + 1 , end , value ) ;
166
+ }
96
167
97
168
// If values[pivotIndex] is equal to value then return `pivotIndex`.
98
169
// Else if values[pivotIndex] is greater than the value, then
0 commit comments