1
- import { computeLexicographicDistance } from "./util.js" ;
2
-
3
1
/**
4
2
* Returns true if the provided age meets the minimum US voting age and false otherwise.
5
3
*
6
4
* @param age The age to check.
7
5
* @return True if the age corresponds to a voting age and false otherwise.
8
6
*/
9
7
export function canVote ( age : number ) : boolean {
8
+ if ( age >= 18 ) {
9
+ console . log ( "You can Vote" ) ;
10
+ return true ;
11
+ } else console . log ( "you cannot vote" ) ;
10
12
return false ;
11
13
}
12
14
13
15
/**
14
16
* Compares two strings lexicographically.
15
17
*
16
- * @param a The first ` string` to compare.
17
- * @param b The second ` string` to compare.
18
+ * @param a The first string to compare.
19
+ * @param b The second string to compare.
18
20
* @return -1 if a is less than b, 1 if a is greater than b, and 0 otherwise.
19
21
*/
20
22
export function compareStrings ( a : string , b : string ) : number {
21
- // The distance will be a number less than 0 if string `a` is lexicographically less than `b` , 1
23
+ // The distance will be a number less than 0 if string a is lexicographically less than b , 1
22
24
// if it is greater, and 0 if the strings are equal.
23
- const distance = computeLexicographicDistance ( a , b ) ;
24
-
25
25
// TODO(you): Finish this method.
26
26
27
- return 0 ;
27
+ if ( a === b ) {
28
+ return 0 ;
29
+ } else if ( a > b ) {
30
+ return 1 ;
31
+ } else return - 1 ;
28
32
}
29
33
30
34
/**
@@ -37,17 +41,52 @@ export function compareStrings(a: string, b: string): number {
37
41
* @return The letter grade ("A+", "A", "A-", "B+", etc.).
38
42
*/
39
43
export function convertGpaToLetterGrade ( gpa : number ) : string {
40
- return "F" ;
44
+ switch ( true ) {
45
+ case gpa >= 97 :
46
+ return "A+" ;
47
+ case gpa >= 93 && gpa <= 96 :
48
+ return "A" ;
49
+ case gpa >= 90 && gpa <= 92 :
50
+ return "A-" ;
51
+ case gpa >= 87 && gpa <= 89 :
52
+ return "B+" ;
53
+ case gpa >= 83 && gpa <= 86 :
54
+ return "B" ;
55
+ case gpa >= 80 && gpa <= 82 :
56
+ return "B-" ;
57
+ case gpa >= 77 && gpa <= 79 :
58
+ return "C+" ;
59
+ case gpa >= 73 && gpa <= 76 :
60
+ return "C" ;
61
+ case gpa >= 70 && gpa <= 72 :
62
+ return "C-" ;
63
+ case gpa >= 67 && gpa <= 69 :
64
+ return "D+" ;
65
+ case gpa >= 65 && gpa <= 66 :
66
+ return "D" ;
67
+ default :
68
+ return "F" ;
69
+ }
41
70
}
42
-
43
71
/**
44
- * Computes the factorial of the given value of `n` .
72
+ * Computes the factorial of the given value of n .
45
73
*
46
74
* @param n The value for which to compute the factorial.
47
75
* @return The factorial of n.
48
76
*/
49
77
export function computeFactorial ( n : number ) : number {
50
- return 0 ;
78
+ if ( n === 1 || n === 0 ) {
79
+ return 1 ;
80
+ } else {
81
+ let factorial = 1 ; //initialize variable
82
+ for (
83
+ let i = 2 ;
84
+ i <= n ;
85
+ i ++ // i starts at 2 and increments until it is equal to n
86
+ )
87
+ factorial = factorial * i ; // factorial loops by multiplying with i until it = n
88
+ return factorial ;
89
+ }
51
90
}
52
91
53
92
/**
@@ -57,17 +96,45 @@ export function computeFactorial(n: number): number {
57
96
* @return The sum of all the values.
58
97
*/
59
98
export function addNumbers ( values : number [ ] ) : number {
60
- return 0 ;
99
+ let sum = 0 ; // initialize variable
100
+
101
+ // eslint-disable-next-line @typescript-eslint/prefer-for-of
102
+ for ( let i = 0 ; i < values . length ; i ++ ) {
103
+ // loop starting 0 as long as i is less than the length of values array increment
104
+ sum = sum + values [ i ] ; // add the values in the array;
105
+ }
106
+ return sum ;
61
107
}
62
108
63
109
/**
64
- * Returns an array of the first `n` Fibonacci numbers starting from 1.
110
+ * Returns an array of the first n Fibonacci numbers starting from 1.
65
111
*
66
- * @param n The first `n` of Fibonacci values to compute.
67
- * @return An array containing the first `n` Fibonacci values.
112
+ * @param n The first n of Fibonacci values to compute.
113
+ * @return An array containing the first n Fibonacci values.
68
114
*/
69
115
export function getFirstNFibonacciNumbers ( n : number ) : number [ ] {
70
- return [ ] ;
116
+ const array : number [ ] = [ ] ; // takes number and converts to array
117
+ if ( n === 0 ) {
118
+ // base case for array 0
119
+ array . push ( 0 ) ;
120
+ return array ;
121
+ }
122
+ if ( n === 1 ) {
123
+ // base case for array 1
124
+ array . push ( 0 ) ;
125
+ array . push ( 1 ) ;
126
+ return array ;
127
+ }
128
+ array . push ( 0 ) ;
129
+ array . push ( 1 ) ;
130
+
131
+ // starting at 2 increment i until it is = n
132
+ for ( let i = 2 ; i <= n ; i ++ ) {
133
+ const Fibonacci = array [ i - 1 ] + array [ i - 2 ] ; //add the numbers 2 positions behind to get current position
134
+ array . push ( Fibonacci ) ;
135
+ }
136
+
137
+ return array ;
71
138
}
72
139
73
140
/**
@@ -94,9 +161,14 @@ export function binarySearch(
94
161
95
162
// TODO(you): Finish implementing this algorithm
96
163
97
- // If values[pivotIndex] is equal to value then return ` pivotIndex` .
164
+ // If values[pivotIndex] is equal to value then return pivotIndex.
98
165
// 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.
166
+ // call binarySearch(values, start, pivotIndex - 1, value) and return its value;
167
+ // Else call binarySearch(values, pivotIndex + 1, end, value) and return its value.
168
+ if ( values [ pivotIndex ] === pivotIndex ) return pivotIndex ;
169
+ else if ( values [ pivotIndex ] > pivotIndex )
170
+ return binarySearch ( values , start , pivotIndex - 1 , value ) ;
171
+ else return binarySearch ( values , pivotIndex + 1 , end , value ) ;
172
+
101
173
return - 1 ;
102
174
}
0 commit comments