@@ -7,24 +7,30 @@ 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
- return false ;
10
+ if ( age >= 18 ) {
11
+ return true ;
12
+ } else return false ;
11
13
}
12
14
13
15
/**
14
16
* Compares two strings lexicographically.
15
17
*
16
- * @param a The first ` string` to compare.
17
- * @param b The second ` string` to compare.
18
+ * @param a The first string to compare.
19
+ * @param b The second string to compare.
18
20
* @return -1 if a is less than b, 1 if a is greater than b, and 0 otherwise.
19
21
*/
20
22
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.
23
+ // The distance will be a number less than 0 if string a is lexicographically less than b,
24
+ // greater than 0 if a is greater, and 0 if the strings are equal.
23
25
const distance = computeLexicographicDistance ( a , b ) ;
24
26
25
- // TODO(you): Finish this method.
26
-
27
- return 0 ;
27
+ if ( distance < 0 ) {
28
+ return - 1 ;
29
+ } else if ( distance > 0 ) {
30
+ return 1 ;
31
+ } else {
32
+ return 0 ;
33
+ }
28
34
}
29
35
30
36
/**
@@ -37,7 +43,27 @@ export function compareStrings(a: string, b: string): number {
37
43
* @return The letter grade ("A+", "A", "A-", "B+", etc.).
38
44
*/
39
45
export function convertGpaToLetterGrade ( gpa : number ) : string {
40
- return "F" ;
46
+ if ( gpa === 4 ) {
47
+ return "A" ;
48
+ } else if ( gpa >= 3.7 ) {
49
+ return "A-" ;
50
+ } else if ( gpa >= 3.3 ) {
51
+ return "B+" ;
52
+ } else if ( gpa >= 3.0 ) {
53
+ return "B" ;
54
+ } else if ( gpa >= 2.7 ) {
55
+ return "B-" ;
56
+ } else if ( gpa >= 2.3 ) {
57
+ return "C+" ;
58
+ } else if ( gpa >= 2.0 ) {
59
+ return "C" ;
60
+ } else if ( gpa >= 1.7 ) {
61
+ return "C-" ;
62
+ } else if ( gpa >= 1.3 ) {
63
+ return "D+" ;
64
+ } else if ( gpa >= 1.0 ) {
65
+ return "D" ;
66
+ } else return "F" ;
41
67
}
42
68
43
69
/**
@@ -47,7 +73,10 @@ export function convertGpaToLetterGrade(gpa: number): string {
47
73
* @return The factorial of n.
48
74
*/
49
75
export function computeFactorial ( n : number ) : number {
50
- return 0 ;
76
+ if ( n < 0 ) {
77
+ throw new Error ( "Negative numbers are invalid." ) ;
78
+ }
79
+ return n <= 1 ? 1 : n * computeFactorial ( n - 1 ) ;
51
80
}
52
81
53
82
/**
@@ -57,17 +86,27 @@ export function computeFactorial(n: number): number {
57
86
* @return The sum of all the values.
58
87
*/
59
88
export function addNumbers ( values : number [ ] ) : number {
60
- return 0 ;
89
+ return values . reduce ( ( sum , current ) => sum + current , 0 ) ;
61
90
}
62
91
63
92
/**
64
93
* Returns an array of the first `n` Fibonacci numbers starting from 1.
65
94
*
66
- * @param n The first `n` of Fibonacci values to compute.
95
+ * @param n The first `n` Fibonacci values to compute.
67
96
* @return An array containing the first `n` Fibonacci values.
68
97
*/
69
98
export function getFirstNFibonacciNumbers ( n : number ) : number [ ] {
70
- return [ ] ;
99
+ if ( n <= 0 ) return [ ] ;
100
+ if ( n === 1 ) return [ 1 ] ;
101
+
102
+ const fibonacci : number [ ] = [ 1 , 1 ] ;
103
+
104
+ for ( let i = 2 ; i < n ; i ++ ) {
105
+ const nextFibonacci = fibonacci [ i - 1 ] + fibonacci [ i - 2 ] ;
106
+ fibonacci . push ( nextFibonacci ) ;
107
+ }
108
+
109
+ return fibonacci ;
71
110
}
72
111
73
112
/**
@@ -90,13 +129,12 @@ export function binarySearch(
90
129
return - 1 ;
91
130
}
92
131
93
- const pivotIndex = Math . floor ( ( start + end ) / 2 ) ; // The index in the middle of the array.
94
-
95
- // TODO(you): Finish implementing this algorithm
96
-
97
- // If values[pivotIndex] is equal to value then return `pivotIndex`.
98
- // Else if values[pivotIndex] is greater than the value, then
99
- // call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
100
- // Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
101
- return - 1 ;
132
+ const pivotIndex = Math . floor ( ( start + end ) / 2 ) ;
133
+ if ( values [ pivotIndex ] === value ) {
134
+ return pivotIndex ;
135
+ } else if ( values [ pivotIndex ] > value ) {
136
+ return binarySearch ( values , start , pivotIndex - 1 , value ) ;
137
+ } else {
138
+ return binarySearch ( values , pivotIndex + 1 , end , value ) ;
139
+ }
102
140
}
0 commit comments