@@ -7,7 +7,11 @@ 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
+ }
11
15
}
12
16
13
17
/**
@@ -21,10 +25,13 @@ export function compareStrings(a: string, b: string): number {
21
25
// The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
22
26
// if it is greater, and 0 if the strings are equal.
23
27
const distance = computeLexicographicDistance ( a , b ) ;
24
-
25
- // TODO(you): Finish this method.
26
-
27
- return 0 ;
28
+ if ( a < b ) {
29
+ return - 1 ;
30
+ } else if ( a > b ) {
31
+ return 1 ;
32
+ } else {
33
+ return 0 ;
34
+ }
28
35
}
29
36
30
37
/**
@@ -37,7 +44,29 @@ 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 >= 3.7 ) {
50
+ return "A-" ;
51
+ } else if ( gpa >= 3.3 ) {
52
+ return "B+" ;
53
+ } else if ( gpa >= 3.0 ) {
54
+ return "B" ;
55
+ } else if ( gpa >= 2.7 ) {
56
+ return "B-" ;
57
+ } else if ( gpa >= 2.3 ) {
58
+ return "C+" ;
59
+ } else if ( gpa >= 2.0 ) {
60
+ return "C" ;
61
+ } else if ( gpa >= 1.7 ) {
62
+ return "C-" ;
63
+ } else if ( gpa >= 1.3 ) {
64
+ return "D+" ;
65
+ } else if ( gpa >= 1.0 ) {
66
+ return "D" ;
67
+ } else {
68
+ return "F" ;
69
+ }
41
70
}
42
71
43
72
/**
@@ -47,7 +76,11 @@ export function convertGpaToLetterGrade(gpa: number): string {
47
76
* @return The factorial of n.
48
77
*/
49
78
export function computeFactorial ( n : number ) : number {
50
- return 0 ;
79
+ let result = 1 ;
80
+ for ( let i = 1 ; i <= n ; i ++ ) {
81
+ result *= i ;
82
+ }
83
+ return result ;
51
84
}
52
85
53
86
/**
@@ -57,7 +90,11 @@ export function computeFactorial(n: number): number {
57
90
* @return The sum of all the values.
58
91
*/
59
92
export function addNumbers ( values : number [ ] ) : number {
60
- return 0 ;
93
+ let total = 0 ;
94
+ for ( const value of values ) {
95
+ total += value ;
96
+ }
97
+ return total ;
61
98
}
62
99
63
100
/**
@@ -67,7 +104,18 @@ export function addNumbers(values: number[]): number {
67
104
* @return An array containing the first `n` Fibonacci values.
68
105
*/
69
106
export function getFirstNFibonacciNumbers ( n : number ) : number [ ] {
70
- return [ ] ;
107
+ if ( n === 0 ) {
108
+ return [ ] ;
109
+ }
110
+ if ( n === 1 ) {
111
+ return [ 1 ] ;
112
+ }
113
+ const fibonacciSequence : number [ ] = [ 1 , 1 ] ;
114
+ for ( let i = 2 ; i < n ; i ++ ) {
115
+ const newNumber = fibonacciSequence [ i - 1 ] + fibonacciSequence [ i - 2 ] ;
116
+ fibonacciSequence . push ( newNumber ) ;
117
+ }
118
+ return fibonacciSequence . slice ( 0 , n ) ;
71
119
}
72
120
73
121
/**
@@ -90,13 +138,12 @@ export function binarySearch(
90
138
return - 1 ;
91
139
}
92
140
93
- const pivotIndex = Math . floor ( ( start + end ) / 2 ) ; // The index in the middle of the array.
94
-
95
- // TODO(you): Finish implementing this algorithm
96
-
97
- // If values[pivotIndex] is equal to value then return `pivotIndex`.
98
- // Else if values[pivotIndex] is greater than the value, then
99
- // call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
100
- // Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
101
- return - 1 ;
141
+ const pivotIndex = Math . floor ( ( start + end ) / 2 ) ;
142
+ if ( values [ pivotIndex ] === value ) {
143
+ return pivotIndex ;
144
+ } else if ( values [ pivotIndex ] > value ) {
145
+ return binarySearch ( values , start , pivotIndex - 1 , value ) ;
146
+ } else {
147
+ return binarySearch ( values , pivotIndex + 1 , end , value ) ;
148
+ }
102
149
}
0 commit comments