@@ -7,9 +7,13 @@ 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 ;
11
- }
12
10
11
+ if ( age >= 18 ) {
12
+ return true ;
13
+ } else {
14
+ return false ;
15
+ }
16
+ }
13
17
/**
14
18
* Compares two strings lexicographically.
15
19
*
@@ -24,7 +28,13 @@ export function compareStrings(a: string, b: string): number {
24
28
25
29
// TODO(you): Finish this method.
26
30
27
- return 0 ;
31
+ if ( a < b ) {
32
+ return - 1 ;
33
+ } if ( a > b ) {
34
+ return 1 ;
35
+ } else {
36
+ return 0 ;
37
+ }
28
38
}
29
39
30
40
/**
@@ -37,7 +47,31 @@ export function compareStrings(a: string, b: string): number {
37
47
* @return The letter grade ("A+", "A", "A-", "B+", etc.).
38
48
*/
39
49
export function convertGpaToLetterGrade ( gpa : number ) : string {
40
- return "F" ;
50
+ if ( gpa >= 97 ) {
51
+ return "A+"
52
+ } if ( gpa >= 93 ) {
53
+ return "A"
54
+ } if ( gpa >= 90 ) {
55
+ return "A-"
56
+ } if ( gpa >= 87 ) {
57
+ return "B+"
58
+ } if ( gpa >= 83 ) {
59
+ return "B"
60
+ } if ( gpa >= 80 ) {
61
+ return "B-"
62
+ } if ( gpa >= 77 ) {
63
+ return "C+"
64
+ } if ( gpa >= 73 ) {
65
+ return "C"
66
+ } if ( gpa >= 70 ) {
67
+ return "C-" ;
68
+ } if ( gpa >= 67 ) {
69
+ return "D+" ;
70
+ } if ( gpa >= 65 ) {
71
+ return "D-" ;
72
+ } else {
73
+ return "F"
74
+ }
41
75
}
42
76
43
77
/**
@@ -47,7 +81,11 @@ export function convertGpaToLetterGrade(gpa: number): string {
47
81
* @return The factorial of n.
48
82
*/
49
83
export function computeFactorial ( n : number ) : number {
50
- return 0 ;
84
+ let totalValue = 1
85
+ for ( let i = 1 ; n >= i ; i ++ ) {
86
+ totalValue *= i
87
+ }
88
+ return totalValue ;
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 ( let i = 0 ; i < values . length ; i ++ ) {
100
+ sum += values [ i ] ;
101
+ }
102
+ return sum ;
61
103
}
62
104
63
105
/**
@@ -67,7 +109,17 @@ 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 array = [ n ]
113
+
114
+ if ( n <= 0 ) {
115
+ return array ;
116
+ }
117
+
118
+ for ( let i = 2 ; i < n ; i ++ ) {
119
+ let cont = array [ i - 1 ] + array [ i - 2 ] ;
120
+ array . push ( cont )
121
+ }
122
+ return array ;
71
123
}
72
124
73
125
/**
@@ -93,10 +145,15 @@ export function binarySearch(
93
145
const pivotIndex = Math . floor ( ( start + end ) / 2 ) ; // The index in the middle of the array.
94
146
95
147
// TODO(you): Finish implementing this algorithm
96
-
148
+ if ( values [ pivotIndex ] === value ) {
149
+ return pivotIndex ;
150
+ } if ( values [ pivotIndex ] < value ) {
151
+ return binarySearch ( values , start , pivotIndex - 1 , value ) ;
152
+ } else {
153
+ return binarySearch ( values , pivotIndex - 1 , end , value ) ;
154
+ }
97
155
// If values[pivotIndex] is equal to value then return `pivotIndex`.
98
156
// Else if values[pivotIndex] is greater than the value, then
99
157
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
100
158
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
101
- return - 1 ;
102
159
}
0 commit comments