@@ -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 false ;
11
+ else return true ;
11
12
}
12
13
13
14
/**
@@ -17,14 +18,14 @@ export function canVote(age: number): boolean {
17
18
* @param b The second `string` to compare.
18
19
* @return -1 if a is less than b, 1 if a is greater than b, and 0 otherwise.
19
20
*/
20
- export function compareStrings ( a : string , b : string ) : number {
21
+ 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 ) return - 1 ;
26
+ else if ( distance > 0 ) return 1 ;
27
+ else return 0 ;
25
28
// TODO(you): Finish this method.
26
-
27
- return 0 ;
28
29
}
29
30
30
31
/**
@@ -37,7 +38,27 @@ export function compareStrings(a: string, b: string): number {
37
38
* @return The letter grade ("A+", "A", "A-", "B+", etc.).
38
39
*/
39
40
export function convertGpaToLetterGrade ( gpa : number ) : string {
40
- return "F" ;
41
+ if ( gpa >= 4.0 ) {
42
+ return "A" ;
43
+ } else if ( gpa >= 3.7 ) {
44
+ return "A-" ;
45
+ } else if ( gpa >= 3.3 ) {
46
+ return "B+" ;
47
+ } else if ( gpa >= 3.0 ) {
48
+ return "B" ;
49
+ } else if ( gpa >= 2.7 ) {
50
+ return "B-" ;
51
+ } else if ( gpa >= 2.3 ) {
52
+ return "C+" ;
53
+ } else if ( gpa >= 2.0 ) {
54
+ return "C" ;
55
+ } else if ( gpa >= 1.7 ) {
56
+ return "C-" ;
57
+ } else if ( gpa >= 1.3 ) {
58
+ return "D+" ;
59
+ } else if ( gpa >= 1.0 ) {
60
+ return "D" ;
61
+ } else return "F" ;
41
62
}
42
63
43
64
/**
@@ -47,7 +68,12 @@ export function convertGpaToLetterGrade(gpa: number): string {
47
68
* @return The factorial of n.
48
69
*/
49
70
export function computeFactorial ( n : number ) : number {
50
- return 0 ;
71
+ let result = 1 ;
72
+ for ( let i = 1 ; i <= n ; i ++ ) {
73
+ result *= i ;
74
+ }
75
+
76
+ return result ;
51
77
}
52
78
53
79
/**
@@ -56,8 +82,13 @@ export function computeFactorial(n: number): number {
56
82
* @param values The values to sum.
57
83
* @return The sum of all the values.
58
84
*/
85
+
59
86
export function addNumbers ( values : number [ ] ) : number {
60
- return 0 ;
87
+ let sum = 0 ;
88
+ for ( const value of values ) {
89
+ sum = sum + value ;
90
+ }
91
+ return sum ;
61
92
}
62
93
63
94
/**
@@ -66,8 +97,20 @@ export function addNumbers(values: number[]): number {
66
97
* @param n The first `n` of Fibonacci values to compute.
67
98
* @return An array containing the first `n` Fibonacci values.
68
99
*/
100
+
69
101
export function getFirstNFibonacciNumbers ( n : number ) : number [ ] {
70
- return [ ] ;
102
+ //return [];
103
+ if ( n <= 0 ) return [ ] ;
104
+ if ( n === 1 ) return [ 1 ] ;
105
+
106
+ const fibonacci : number [ ] = [ 1 , 1 ] ;
107
+
108
+ for ( let i = 2 ; i < n ; i ++ ) {
109
+ const numberFibonacci = fibonacci [ i - 1 ] + fibonacci [ i - 2 ] ;
110
+ fibonacci . push ( numberFibonacci ) ;
111
+ }
112
+
113
+ return fibonacci ;
71
114
}
72
115
73
116
/**
@@ -92,11 +135,18 @@ export function binarySearch(
92
135
93
136
const pivotIndex = Math . floor ( ( start + end ) / 2 ) ; // The index in the middle of the array.
94
137
138
+ if ( values [ pivotIndex ] === value ) {
139
+ return pivotIndex ;
140
+ } else if ( values [ pivotIndex ] > value ) {
141
+ return binarySearch ( values , start , pivotIndex - 1 , value ) ;
142
+ } else {
143
+ return binarySearch ( values , pivotIndex + 1 , end , value ) ;
144
+ }
145
+
95
146
// TODO(you): Finish implementing this algorithm
96
147
97
148
// If values[pivotIndex] is equal to value then return `pivotIndex`.
98
149
// Else if values[pivotIndex] is greater than the value, then
99
150
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
100
151
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
101
- return - 1 ;
102
152
}
0 commit comments