1
1
import { computeLexicographicDistance } from "./util.js" ;
2
2
3
- /**
3
+ /** (Q1)
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
export function canVote ( age : number ) : boolean {
10
- return false ;
10
+ return age >= 18 ;
11
11
}
12
12
13
- /**
13
+ const age = 12 ;
14
+ let checkAge ;
15
+
16
+ if ( canVote ( age ) ) {
17
+ checkAge = true ;
18
+ } else {
19
+ checkAge = false ;
20
+ }
21
+
22
+ console . log ( checkAge ) ;
23
+
24
+ /** (Q2)
14
25
* Compares two strings lexicographically.
15
26
*
16
27
* @param a The first `string` to compare.
17
28
* @param b The second `string` to compare.
18
29
* @return -1 if a is less than b, 1 if a is greater than b, and 0 otherwise.
19
30
*/
20
31
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
22
- // if it is greater, and 0 if the strings are equal.
23
32
const distance = computeLexicographicDistance ( a , b ) ;
33
+ return distance ;
34
+ }
24
35
25
- // TODO(you): Finish this method.
26
-
27
- return 0 ;
36
+ export function computeLexicographicDistance ( a : string , b : string ) : number {
37
+ //I literally used export function again because typescript kept telling me
38
+ // that my return statements needed to be inside the body of a function.
39
+ // I'm a bit skeptical because we are supposed to be DRY! and the only thing I
40
+ // changed was the function name. But it worked. I'm just leaving this note
41
+ // to remind myself to ask for clarification about this.
42
+ if ( a < b ) {
43
+ return - 1 ;
44
+ } else if ( a > b ) {
45
+ return 1 ;
46
+ } else {
47
+ return 0 ;
48
+ }
28
49
}
29
50
30
- /**
51
+ const result = compareStrings ( "Kimberlee" , "haldane" ) ;
52
+ console . log ( result ) ;
53
+
54
+ //If I am understanding question 2 correctly, because the first function
55
+ // 'compareStrings' calls on a second function 'computeLexicographicDistance',
56
+ // I needed to set up both functions. The first function calls the second
57
+ // and the second function does the actual calculation and determines if each string
58
+ // is one of this set (-1, 1, 0). Testing is when the first function comes
59
+ //back into play, comparing two strings and returning a numerical result. Sorry
60
+ // for the long note, it took me a VERY long time to get to this understanding.
61
+ // I just hope I am right.
62
+
63
+ /** (Q3)
31
64
* Converts a GPA on the 4.0 scale to the corresponding letter grade using the college board
32
65
* scale. See
33
66
* https://bigfuture.collegeboard.org/plan-for-college/college-basics/how-to-convert-gpa-4.0-scale
@@ -36,31 +69,86 @@ export function compareStrings(a: string, b: string): number {
36
69
* @param gpa The GPA value.
37
70
* @return The letter grade ("A+", "A", "A-", "B+", etc.).
38
71
*/
72
+ export function myGrade ( grade : number ) : string {
73
+ const gpa = convertGpaToLetterGrade ( grade ) ;
74
+ return gpa ;
75
+ }
76
+
77
+ //I followed the same logic as question 2. I can already tell that
78
+ // there may be many simpler ways to build this function. I am just
79
+ // happy it came out with no errors and will make a note to play around
80
+ // with this and see if I can't get it to look better (simpler/cleaner).
81
+
39
82
export function convertGpaToLetterGrade ( gpa : number ) : string {
40
- return "F" ;
83
+ if ( gpa < 65 ) {
84
+ return "F" ;
85
+ } else if ( gpa < 67 && gpa >= 65 ) {
86
+ return "D" ;
87
+ } else if ( gpa < 70 && gpa >= 67 ) {
88
+ return "D+" ;
89
+ } else if ( gpa < 73 && gpa >= 70 ) {
90
+ return "C-" ;
91
+ } else if ( gpa < 77 && gpa >= 73 ) {
92
+ return "C" ;
93
+ } else if ( gpa < 80 && gpa >= 77 ) {
94
+ return "C+" ;
95
+ } else if ( gpa < 83 && gpa >= 80 ) {
96
+ return "B-" ;
97
+ } else if ( gpa < 87 && gpa >= 83 ) {
98
+ return "B" ;
99
+ } else if ( gpa < 90 && gpa >= 87 ) {
100
+ return "B+" ;
101
+ } else if ( gpa < 93 && gpa >= 90 ) {
102
+ return "A-" ;
103
+ } else if ( gpa < 97 && gpa >= 93 ) {
104
+ return "A" ;
105
+ } else if ( gpa <= 100 && gpa >= 97 ) {
106
+ return "A+" ;
107
+ } else {
108
+ return "unable to assess" ;
109
+ }
41
110
}
42
111
43
- /**
112
+ const grade = convertGpaToLetterGrade ( 32 ) ;
113
+ console . log ( grade ) ;
114
+
115
+ /** (Q4)
44
116
* Computes the factorial of the given value of `n`.
45
117
*
46
118
* @param n The value for which to compute the factorial.
47
119
* @return The factorial of n.
48
120
*/
49
121
export function computeFactorial ( n : number ) : number {
50
- return 0 ;
122
+ let result = 1 ;
123
+ for ( let i = 1 ; i <= n ; i ++ ) { // I used i+1 and fell into an infinite loop. My bad!
124
+ result *= i ;
125
+ }
126
+ return result ;
51
127
}
128
+ // Steps I followed: Describe the function, Declare the function, crawl inside the function
129
+ const n = 7 ;
130
+ console . log ( computeFactorial ( n ) ) ;
131
+
52
132
53
- /**
133
+ /** (Q5)
54
134
* Adds all of the provided values and returns the sum.
55
135
*
56
136
* @param values The values to sum.
57
137
* @return The sum of all the values.
58
138
*/
59
139
export function addNumbers ( values : number [ ] ) : number {
60
- return 0 ;
140
+ let total = 0 ;
141
+ for ( const value of values ) {
142
+ total += value ;
143
+ }
144
+ return total ;
61
145
}
62
146
63
- /**
147
+ const numbers = [ 8 , 4 , 6 , 2 , 7 ] ;
148
+ const sum = addNumbers ( numbers ) ;
149
+ console . log ( sum ) ;
150
+
151
+ /** (Q6)
64
152
* Returns an array of the first `n` Fibonacci numbers starting from 1.
65
153
*
66
154
* @param n The first `n` of Fibonacci values to compute.
@@ -70,7 +158,7 @@ export function getFirstNFibonacciNumbers(n: number): number[] {
70
158
return [ ] ;
71
159
}
72
160
73
- /**
161
+ /** (Q7)
74
162
* Finds a value in an array of values.
75
163
*
76
164
* @param values The values to search.
0 commit comments