@@ -7,7 +7,12 @@ 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 {
13
+ return false ;
14
+ }
15
+
11
16
}
12
17
13
18
/**
@@ -23,10 +28,12 @@ export function compareStrings(a: string, b: string): number {
23
28
const distance = computeLexicographicDistance ( a , b ) ;
24
29
25
30
// TODO(you): Finish this method.
31
+ if ( distance < 0 ) {
32
+ return - 1 ;
33
+ }
26
34
27
- return 0 ;
35
+ return distance ;
28
36
}
29
-
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,46 @@ 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
+ } else if ( gpa > 4.0 ) {
50
+ return "A" ;
51
+ } else if ( gpa <= 3.99 && gpa >= 3.7 ) {
52
+ return "A-" ;
53
+ } else if ( gpa <= 3.69 && gpa >= 3.3 ) {
54
+ return "B+" ;
55
+ } else if ( gpa <= 3.29 && gpa >= 3.0 ) {
56
+ return "B" ;
57
+ } else if ( gpa <= 2.99 && gpa >= 2.7 ) {
58
+ return "B-" ;
59
+ } else if ( gpa <= 2.69 && gpa >= 2.3 ) {
60
+ return "C+" ;
61
+ } else if ( gpa <= 2.29 && gpa >= 2.0 ) {
62
+ return "C" ;
63
+ } else if ( gpa <= 1.99 && gpa >= 1.7 ) {
64
+ return "C-" ;
65
+ } else if ( gpa <= 1.69 && gpa >= 1.3 ) {
66
+ return "D+" ;
67
+ } else if ( gpa <= 1.29 && gpa >= 1.0 ) {
68
+ return "D" ;
69
+ } else {
70
+ return "F" ;
71
+ }
41
72
}
42
73
43
- /**
44
- * Computes the factorial of the given value of `n`.
74
+ /**km
75
+ * computes the factorial of the given value of `n`.
45
76
*
46
77
* @param n The value for which to compute the factorial.
47
78
* @return The factorial of n.
48
79
*/
49
80
export function computeFactorial ( n : number ) : number {
50
- return 0 ;
81
+ let product = 1 ;
82
+ for ( let i = 1 ; i <= n ; i ++ ) {
83
+ product *= i ;
84
+ }
85
+
86
+ return product ;
51
87
}
52
88
53
89
/**
@@ -57,7 +93,12 @@ export function computeFactorial(n: number): number {
57
93
* @return The sum of all the values.
58
94
*/
59
95
export function addNumbers ( values : number [ ] ) : number {
60
- return 0 ;
96
+ let sum = 0 ; //initialize variable
97
+
98
+ for ( const value of values ) {
99
+ sum += value ; //adds value to each
100
+ }
101
+ return sum ;
61
102
}
62
103
63
104
/**
@@ -67,7 +108,21 @@ export function addNumbers(values: number[]): number {
67
108
* @return An array containing the first `n` Fibonacci values.
68
109
*/
69
110
export function getFirstNFibonacciNumbers ( n : number ) : number [ ] {
70
- return [ ] ;
111
+ let current = 1 ;
112
+ let prev = 0 ;
113
+
114
+ const numbers = [ ] ;
115
+
116
+ for ( let i = 1 ; i <= n ; i ++ ) {
117
+ numbers . push ( current ) ;
118
+ const nextNum = current + prev ; // 1
119
+ prev = current ; // 1
120
+ current = nextNum
121
+ }
122
+
123
+
124
+
125
+ return numbers ;
71
126
}
72
127
73
128
/**
@@ -86,12 +141,22 @@ export function binarySearch(
86
141
value : number ,
87
142
) : number {
88
143
if ( end < start ) {
144
+
145
+
89
146
// The range is not valid so just return -1.
90
147
return - 1 ;
91
148
}
92
149
93
150
const pivotIndex = Math . floor ( ( start + end ) / 2 ) ; // The index in the middle of the array.
94
-
151
+ if ( values [ pivotIndex ] === value ) {
152
+ return pivotIndex ;
153
+ }
154
+ else if ( values [ pivotIndex ] > value ) {
155
+ return binarySearch ( values , start , pivotIndex - 1 , value ) ;
156
+ }
157
+ else {
158
+ return binarySearch ( values , pivotIndex + 1 , end , value ) ;
159
+ }
95
160
// TODO(you): Finish implementing this algorithm
96
161
97
162
// If values[pivotIndex] is equal to value then return `pivotIndex`.
0 commit comments