@@ -7,7 +7,8 @@ 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 ) return true ;
11
+ else return false ;
11
12
}
12
13
13
14
/**
@@ -21,10 +22,13 @@ export function compareStrings(a: string, b: string): number {
21
22
// The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
22
23
// if it is greater, and 0 if the strings are equal.
23
24
const distance = computeLexicographicDistance ( a , b ) ;
24
-
25
+ if ( distance == 0 ) {
26
+ return 0 ;
27
+ } else if ( distance < 1 ) {
28
+ return - 1 ;
29
+ }
25
30
// TODO(you): Finish this method.
26
-
27
- return 0 ;
31
+ else return 1 ;
28
32
}
29
33
30
34
/**
@@ -37,7 +41,32 @@ export function compareStrings(a: string, b: string): number {
37
41
* @return The letter grade ("A+", "A", "A-", "B+", etc.).
38
42
*/
39
43
export function convertGpaToLetterGrade ( gpa : number ) : string {
40
- return "F" ;
44
+ switch ( true ) {
45
+ case gpa >= 4.0 :
46
+ return "A" ;
47
+ case gpa <= 3.99 && gpa >= 3.7 :
48
+ return "A-" ;
49
+ case gpa >= 3.3 && gpa <= 3.69 :
50
+ return "B+" ;
51
+ case gpa >= 3.0 && gpa <= 3.29 :
52
+ return "B" ;
53
+ case gpa >= 2.7 && gpa <= 2.99 :
54
+ return "B-" ;
55
+ case gpa >= 2.3 && gpa <= 2.69 :
56
+ return "C+" ;
57
+ case gpa >= 2.0 && gpa <= 2.29 :
58
+ return "C" ;
59
+ case gpa >= 1.7 && gpa <= 1.99 :
60
+ return "C-" ;
61
+ case gpa >= 1.3 && gpa <= 1.69 :
62
+ return "D+" ;
63
+ case gpa >= 1.0 && gpa <= 1.29 :
64
+ return "D" ;
65
+ case gpa < 1 :
66
+ return "F" ;
67
+ default :
68
+ return "unknown" ;
69
+ }
41
70
}
42
71
43
72
/**
@@ -47,7 +76,11 @@ export function convertGpaToLetterGrade(gpa: number): string {
47
76
* @return The factorial of n.
48
77
*/
49
78
export function computeFactorial ( n : number ) : number {
50
- return 0 ;
79
+ if ( n == 0 ) return 1 ;
80
+ for ( let i = n - 1 ; i > 0 ; i -- ) {
81
+ n *= i ;
82
+ }
83
+ return n ;
51
84
}
52
85
53
86
/**
@@ -57,7 +90,8 @@ export function computeFactorial(n: number): number {
57
90
* @return The sum of all the values.
58
91
*/
59
92
export function addNumbers ( values : number [ ] ) : number {
60
- return 0 ;
93
+ const sum : number = values . reduce ( ( a , b ) => a + b , 0 ) ;
94
+ return sum ;
61
95
}
62
96
63
97
/**
@@ -67,7 +101,16 @@ export function addNumbers(values: number[]): number {
67
101
* @return An array containing the first `n` Fibonacci values.
68
102
*/
69
103
export function getFirstNFibonacciNumbers ( n : number ) : number [ ] {
70
- return [ ] ;
104
+ const arr = [ 1 , 1 ] ;
105
+
106
+ if ( n === 0 ) return [ ] ;
107
+ if ( n === 1 ) return [ arr [ n ] ] ;
108
+ else if ( n > 1 ) {
109
+ for ( let i = 3 ; i <= n ; i ++ ) {
110
+ arr [ i - 1 ] = arr [ i - 3 ] + arr [ i - 2 ] ;
111
+ }
112
+ }
113
+ return arr ;
71
114
}
72
115
73
116
/**
@@ -85,18 +128,21 @@ export function binarySearch(
85
128
end : number ,
86
129
value : number ,
87
130
) : number {
131
+ const pivotIndex = Math . floor ( ( start + end ) / 2 ) ; // The index in the middle of the array.
132
+
88
133
if ( end < start ) {
89
134
// The range is not valid so just return -1.
90
135
return - 1 ;
136
+ } else if ( values [ pivotIndex ] == value ) return pivotIndex ;
137
+ else if ( values [ pivotIndex ] > value ) {
138
+ return binarySearch ( values , start , pivotIndex - 1 , value ) ;
139
+ } else {
140
+ return binarySearch ( values , pivotIndex + 1 , end , value ) ;
91
141
}
92
-
93
- const pivotIndex = Math . floor ( ( start + end ) / 2 ) ; // The index in the middle of the array.
94
-
95
142
// TODO(you): Finish implementing this algorithm
96
143
97
144
// If values[pivotIndex] is equal to value then return `pivotIndex`.
98
145
// Else if values[pivotIndex] is greater than the value, then
99
146
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
100
147
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
101
- return - 1 ;
102
148
}
0 commit comments