@@ -7,7 +7,10 @@ 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 false ;
11
+ if ( age >= 18 ) return true ;
12
+
13
+ return true ;
11
14
}
12
15
13
16
/**
@@ -23,8 +26,11 @@ export function compareStrings(a: string, b: string): number {
23
26
const distance = computeLexicographicDistance ( a , b ) ;
24
27
25
28
// TODO(you): Finish this method.
29
+ if ( distance < 0 ) {
30
+ return - 1 ;
31
+ }
26
32
27
- return 0 ;
33
+ return distance ;
28
34
}
29
35
30
36
/**
@@ -37,6 +43,18 @@ 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 {
46
+ if ( gpa > 4.0 ) return "A+" ;
47
+ if ( gpa >= 3.7 ) return "A" ;
48
+ if ( gpa >= 3.3 ) return "A-" ;
49
+ if ( gpa >= 3.0 ) return "B+" ;
50
+ if ( gpa >= 2.7 ) return "B" ;
51
+ if ( gpa >= 2.3 ) return "B-" ;
52
+ if ( gpa >= 2.0 ) return "C+" ;
53
+ if ( gpa >= 1.7 ) return "C" ;
54
+ if ( gpa >= 1.3 ) return "C-" ;
55
+ if ( gpa >= 1.0 ) return "D+" ;
56
+ if ( gpa >= 0.0 ) return "D" ;
57
+
40
58
return "F" ;
41
59
}
42
60
@@ -47,7 +65,12 @@ export function convertGpaToLetterGrade(gpa: number): string {
47
65
* @return The factorial of n.
48
66
*/
49
67
export function computeFactorial ( n : number ) : number {
50
- return 0 ;
68
+ if ( n === 1 || n === 0 ) return 1 ;
69
+
70
+ let nums = 1 ;
71
+ for ( let i = 2 ; i <= n ; i ++ ) nums *= i ;
72
+
73
+ return nums ;
51
74
}
52
75
53
76
/**
@@ -57,7 +80,13 @@ export function computeFactorial(n: number): number {
57
80
* @return The sum of all the values.
58
81
*/
59
82
export function addNumbers ( values : number [ ] ) : number {
60
- return 0 ;
83
+ let sum = 0 ;
84
+
85
+ for ( const value of values ) {
86
+ sum += value ;
87
+ }
88
+
89
+ return sum ;
61
90
}
62
91
63
92
/**
@@ -87,6 +116,7 @@ export function binarySearch(
87
116
) : number {
88
117
if ( end < start ) {
89
118
// The range is not valid so just return -1.
119
+
90
120
return - 1 ;
91
121
}
92
122
0 commit comments