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
10
export function canVote ( age : number ) : boolean {
11
- return false ;
12
- if ( age >= 18 ) {
13
- return true ;
14
- } else {
15
- return false ;
16
- }
17
- console . log ( age >= 18 ) ;
18
-
11
+ if ( age >= 18 ) {
12
+ return true ;
13
+ } else {
14
+ return false ;
15
+ }
19
16
}
20
17
/**
21
18
* Compares two strings lexicographically.
@@ -27,21 +24,17 @@ console.log(age >= 18);
27
24
export function compareStrings ( a : string , b : string ) : number {
28
25
// The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
29
26
// if it is greater, and 0 if the strings are equal.
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
- }
27
+ const distance = computeLexicographicDistance ( a , b ) ;
40
28
41
- }
42
29
//This is just an idea(That's probably wrong)/
43
30
// TODO(you): Finish this method.
44
-
31
+ let stringLengthCompare = 0 ;
32
+ if ( distance > 0 ) {
33
+ stringLengthCompare = 1 ; //equal distances
34
+ } else if ( distance < 0 ) {
35
+ stringLengthCompare = - 1 ; //unequal distances
36
+ }
37
+ return stringLengthCompare ;
45
38
}
46
39
47
40
/**
@@ -54,31 +47,29 @@ export function compareStrings(a: string, b: string): number {
54
47
* @return The letter grade ("A+", "A", "A-", "B+", etc.).
55
48
*/
56
49
export function convertGpaToLetterGrade ( gpa : number ) : string {
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
-
50
+ if ( gpa == 4.0 ) {
51
+ return "A" ;
52
+ } else if ( gpa <= 3.99 && gpa >= 3.7 ) {
53
+ return "A-" ;
54
+ } else if ( gpa <= 3.69 && gpa >= 3.3 ) {
55
+ return "B+" ;
56
+ } else if ( gpa <= 3.29 && gpa >= 3.0 ) {
57
+ return "B" ;
58
+ } else if ( gpa <= 2.99 && gpa >= 2.7 ) {
59
+ return "B-" ;
60
+ } else if ( gpa <= 2.69 && gpa >= 2.3 ) {
61
+ return "C+" ;
62
+ } else if ( gpa <= 2.29 && gpa >= 2.0 ) {
63
+ return "C" ;
64
+ } else if ( gpa <= 1.99 && gpa >= 1.7 ) {
65
+ return "C-" ;
66
+ } else if ( gpa <= 1.69 && gpa >= 1.3 ) {
67
+ return "D+" ;
68
+ } else if ( gpa <= 1.29 && gpa >= 1.0 ) {
69
+ return "D" ;
70
+ } else {
71
+ return "F" ;
72
+ }
82
73
}
83
74
84
75
/**
@@ -88,7 +79,6 @@ if (gpa == 4.0) {
88
79
* @return The factorial of n.
89
80
*/
90
81
export function computeFactorial ( n : number ) : number {
91
- return 0 ;
92
82
let product = 1 ;
93
83
for ( let i = 1 ; 1 <= n ; i ++ ) {
94
84
product *= i ;
@@ -104,9 +94,9 @@ export function computeFactorial(n: number): number {
104
94
* @return The sum of all the values.
105
95
*/
106
96
export function addNumbers ( values : number [ ] ) : number {
107
- return 0 ;
108
- let sum = 0 ; for ( const value of values ) {
109
- sum + value ;
97
+ let sum = 0 ;
98
+ for ( const value of values ) {
99
+ sum += value ;
110
100
}
111
101
return sum ;
112
102
}
@@ -125,7 +115,7 @@ export function getFirstNFibonacciNumbers(n: number): number[] {
125
115
const fibonacci : number [ ] = [ 1 , 1 ] ; // The function starts with the first two Fibonacci numbers
126
116
127
117
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
118
+ fibonacci [ i ] = fibonacci [ i - 1 ] + fibonacci [ i - 2 ] ; // Here the sum of the preceeding expression(?) is iterated with the number directly preceeding it
129
119
}
130
120
131
121
return fibonacci . slice ( 0 , n ) ; // Return only the first n Fibonacci numbers
@@ -147,26 +137,19 @@ export function binarySearch(
147
137
value : number ,
148
138
) : number {
149
139
if ( end < start ) {
150
-
151
140
return - 1 ;
152
141
}
153
142
154
-
155
-
156
-
157
-
158
143
const pivotIndex = Math . floor ( ( start + end ) / 2 ) ; // The index in the middle of the array.
159
144
160
145
if ( values [ pivotIndex ] === value ) {
161
146
return pivotIndex ;
162
-
163
147
} 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
- }
148
+ return binarySearch ( values , start , pivotIndex - 1 , value ) ;
149
+ return value ;
150
+ } else {
151
+ return binarySearch ( values , pivotIndex + 1 , end , value ) ;
152
+ }
170
153
// TODO(you): Finish implementing this algorithm
171
154
172
155
// If values[pivotIndex] is equal to value then return `pivotIndex`.
0 commit comments