@@ -7,6 +7,9 @@ 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
+ if ( age >= 18 ) {
11
+ return true ;
12
+ }
10
13
return false ;
11
14
}
12
15
@@ -22,9 +25,14 @@ export function compareStrings(a: string, b: string): number {
22
25
// if it is greater, and 0 if the strings are equal.
23
26
const distance = computeLexicographicDistance ( a , b ) ;
24
27
28
+ if ( distance < 0 ) {
29
+ return - 1 ;
30
+ } else if ( distance > 0 ) {
31
+ return 1 ;
32
+ } else {
33
+ return 0 ;
34
+ }
25
35
// TODO(you): Finish this method.
26
-
27
- return 0 ;
28
36
}
29
37
30
38
/**
@@ -37,7 +45,29 @@ export function compareStrings(a: string, b: string): number {
37
45
* @return The letter grade ("A+", "A", "A-", "B+", etc.).
38
46
*/
39
47
export function convertGpaToLetterGrade ( gpa : number ) : string {
40
- return "F" ;
48
+ if ( gpa == 4.0 ) {
49
+ return "A" ;
50
+ } else if ( gpa <= 3.99 && gpa >= 3.7 ) {
51
+ return "A-" ;
52
+ } else if ( gpa <= 3.69 && gpa >= 3.3 ) {
53
+ return "B+" ;
54
+ } else if ( gpa <= 3.29 && gpa >= 3.0 ) {
55
+ return "B" ;
56
+ } else if ( gpa <= 2.99 && gpa >= 2.7 ) {
57
+ return "B-" ;
58
+ } else if ( gpa <= 2.69 && gpa >= 2.3 ) {
59
+ return "C+" ;
60
+ } else if ( gpa <= 2.29 && gpa >= 2.0 ) {
61
+ return "C" ;
62
+ } else if ( gpa <= 1.99 && gpa >= 1.7 ) {
63
+ return "C-" ;
64
+ } else if ( gpa <= 1.69 && gpa >= 1.3 ) {
65
+ return "D+" ;
66
+ } else if ( gpa <= 1.29 && gpa >= 1.0 ) {
67
+ return "D" ;
68
+ } else {
69
+ return "F" ;
70
+ }
41
71
}
42
72
43
73
/**
@@ -47,17 +77,26 @@ export function convertGpaToLetterGrade(gpa: number): string {
47
77
* @return The factorial of n.
48
78
*/
49
79
export function computeFactorial ( n : number ) : number {
50
- return 0 ;
80
+ if ( n == 0 || n == 1 ) {
81
+ return 1 ;
82
+ } else if ( n > 1 ) {
83
+ return n * computeFactorial ( n - 1 ) ;
84
+ }
85
+ return n ;
51
86
}
52
-
87
+ /* 4! = 4 x 3 x 2 x 1 = 24*/
53
88
/**
54
89
* Adds all of the provided values and returns the sum.
55
90
*
56
91
* @param values The values to sum.
57
92
* @return The sum of all the values.
58
93
*/
59
94
export function addNumbers ( values : number [ ] ) : number {
60
- return 0 ;
95
+ let sum = 0 ;
96
+ for ( const value of values ) {
97
+ sum += value ;
98
+ }
99
+ return sum ;
61
100
}
62
101
63
102
/**
@@ -67,7 +106,17 @@ export function addNumbers(values: number[]): number {
67
106
* @return An array containing the first `n` Fibonacci values.
68
107
*/
69
108
export function getFirstNFibonacciNumbers ( n : number ) : number [ ] {
70
- return [ ] ;
109
+ const fibArray : number [ ] = [ ] ;
110
+ let a = 0 ;
111
+ let b = 1 ;
112
+ let c : number ;
113
+ while ( fibArray . length < n ) {
114
+ c = a ;
115
+ a = b ;
116
+ b = c + b ;
117
+ fibArray . push ( a ) ;
118
+ }
119
+ return fibArray ;
71
120
}
72
121
73
122
/**
@@ -94,9 +143,15 @@ export function binarySearch(
94
143
95
144
// TODO(you): Finish implementing this algorithm
96
145
146
+ if ( values [ pivotIndex ] == value ) {
147
+ return pivotIndex ;
148
+ } else if ( values [ pivotIndex ] > value ) {
149
+ return binarySearch ( values , start , pivotIndex - 1 , value ) ;
150
+ } else {
151
+ return binarySearch ( values , pivotIndex + 1 , end , value ) ;
152
+ }
97
153
// If values[pivotIndex] is equal to value then return `pivotIndex`.
98
154
// Else if values[pivotIndex] is greater than the value, then
99
155
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
100
156
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
101
- return - 1 ;
102
157
}
0 commit comments