1
1
import { computeLexicographicDistance } from "./util.js" ;
2
2
3
- /**
3
+ /**
4
4
* Returns true if the provided age meets the minimum US voting age and false otherwise.
5
5
*
6
6
* @param age The age to check.
7
7
* @return True if the age corresponds to a voting age and false otherwise.
8
- */
8
+ */
9
+
9
10
export function canVote ( age : number ) : boolean {
11
+ return false ;
12
+ if ( age >= 18 ) {
13
+ return true ;
14
+ } else {
10
15
return false ;
11
- }
16
+ }
17
+ console . log ( age >= 18 ) ;
12
18
19
+ }
13
20
/**
14
21
* Compares two strings lexicographically.
15
22
*
@@ -20,11 +27,21 @@ export function canVote(age: number): boolean {
20
27
export function compareStrings ( a : string , b : string ) : number {
21
28
// The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
22
29
// if it is greater, and 0 if the strings are equal.
23
- const distance = computeLexicographicDistance ( a , b ) ;
30
+ const distance = computeLexicographicDistance ( a , b ) ;
31
+ distance ; a ; 1 ;
32
+ distance ; b ; 1 ; {
33
+ if ( distance > 0 ) {
34
+ return 1 ; //equal distances
35
+ } else if ( distance < 0 ) {
36
+ return - 1 ; //unequal distances
37
+ } else if ( distance === 0 ) {
38
+ return 0 ;
39
+ }
24
40
41
+ }
42
+ //This is just an idea(That's probably wrong)/
25
43
// TODO(you): Finish this method.
26
-
27
- return 0 ;
44
+
28
45
}
29
46
30
47
/**
@@ -38,6 +55,30 @@ export function compareStrings(a: string, b: string): number {
38
55
*/
39
56
export function convertGpaToLetterGrade ( gpa : number ) : string {
40
57
return "F" ;
58
+ if ( gpa == 4.0 ) {
59
+ return "A" ;
60
+ } else if ( gpa <= 3.99 && gpa >= 3.7 ) {
61
+ return "A-" ;
62
+ } else if ( gpa <= 3.69 && gpa >= 3.3 ) {
63
+ return "B+" ;
64
+ } else if ( gpa <= 3.29 && gpa >= 3.0 ) {
65
+ return "B" ;
66
+ } else if ( gpa <= 2.99 && gpa >= 2.7 ) {
67
+ return "B-" ;
68
+ } else if ( gpa <= 2.69 && gpa >= 2.3 ) {
69
+ return "C+" ;
70
+ } else if ( gpa <= 2.29 && gpa >= 2.0 ) {
71
+ return "C" ;
72
+ } else if ( gpa <= 1.99 && gpa >= 1.7 ) {
73
+ return "C-" ;
74
+ } else if ( gpa <= 1.69 && gpa >= 1.3 ) {
75
+ return "D+" ;
76
+ } else if ( gpa <= 1.29 && gpa >= 1.0 ) {
77
+ return "D" ;
78
+ } else {
79
+ return "F" ;
80
+ }
81
+
41
82
}
42
83
43
84
/**
@@ -48,6 +89,12 @@ export function convertGpaToLetterGrade(gpa: number): string {
48
89
*/
49
90
export function computeFactorial ( n : number ) : number {
50
91
return 0 ;
92
+ let product = 1 ;
93
+ for ( let i = 1 ; 1 <= n ; i ++ ) {
94
+ product *= i ;
95
+ }
96
+
97
+ return product ;
51
98
}
52
99
53
100
/**
@@ -58,6 +105,10 @@ export function computeFactorial(n: number): number {
58
105
*/
59
106
export function addNumbers ( values : number [ ] ) : number {
60
107
return 0 ;
108
+ let sum = 0 ; for ( const value of values ) {
109
+ sum + value ;
110
+ }
111
+ return sum ;
61
112
}
62
113
63
114
/**
@@ -67,7 +118,17 @@ export function addNumbers(values: number[]): number {
67
118
* @return An array containing the first `n` Fibonacci values.
68
119
*/
69
120
export function getFirstNFibonacciNumbers ( n : number ) : number [ ] {
70
- return [ ] ;
121
+ if ( n < 1 ) {
122
+ return [ ] ;
123
+ }
124
+
125
+ const fibonacci : number [ ] = [ 1 , 1 ] ; // The function starts with the first two Fibonacci numbers
126
+
127
+ for ( let i = 2 ; i < n ; i ++ ) {
128
+ fibonacci [ i ] = fibonacci [ i - 1 ] + fibonacci [ i - 2 ] ; // Here the sum of the preceeding expression(?) is iterated with the number directly preceeding it
129
+ }
130
+
131
+ return fibonacci . slice ( 0 , n ) ; // Return only the first n Fibonacci numbers
71
132
}
72
133
73
134
/**
@@ -86,12 +147,26 @@ export function binarySearch(
86
147
value : number ,
87
148
) : number {
88
149
if ( end < start ) {
89
- // The range is not valid so just return -1.
150
+
90
151
return - 1 ;
91
152
}
92
153
154
+
155
+
156
+
157
+
93
158
const pivotIndex = Math . floor ( ( start + end ) / 2 ) ; // The index in the middle of the array.
94
159
160
+ if ( values [ pivotIndex ] === value ) {
161
+ return pivotIndex ;
162
+
163
+ } else if ( values [ pivotIndex ] > value ) {
164
+ return binarySearch ( values , start , pivotIndex - 1 , value ) ;
165
+ return value ;
166
+ } else {
167
+ return ( binarySearch ( values , pivotIndex + 1 , end , value ) ) ;
168
+
169
+ }
95
170
// TODO(you): Finish implementing this algorithm
96
171
97
172
// If values[pivotIndex] is equal to value then return `pivotIndex`.
0 commit comments