@@ -7,9 +7,13 @@ import { computeLexicographicDistance } from "./util.js";
77 * @return True if the age corresponds to a voting age and false otherwise.
88 */
99export function canVote ( age : number ) : boolean {
10- return false ;
11- }
1210
11+ if ( age >= 18 ) {
12+ return true ;
13+ } else {
14+ return false ;
15+ }
16+ }
1317/**
1418 * Compares two strings lexicographically.
1519 *
@@ -24,7 +28,13 @@ export function compareStrings(a: string, b: string): number {
2428
2529 // TODO(you): Finish this method.
2630
27- return 0 ;
31+ if ( a < b ) {
32+ return - 1 ;
33+ } if ( a > b ) {
34+ return 1 ;
35+ } else {
36+ return 0 ;
37+ }
2838}
2939
3040/**
@@ -37,7 +47,31 @@ export function compareStrings(a: string, b: string): number {
3747 * @return The letter grade ("A+", "A", "A-", "B+", etc.).
3848 */
3949export function convertGpaToLetterGrade ( gpa : number ) : string {
40- return "F" ;
50+ if ( gpa >= 97 ) {
51+ return "A+"
52+ } if ( gpa >= 93 ) {
53+ return "A"
54+ } if ( gpa >= 90 ) {
55+ return "A-"
56+ } if ( gpa >= 87 ) {
57+ return "B+"
58+ } if ( gpa >= 83 ) {
59+ return "B"
60+ } if ( gpa >= 80 ) {
61+ return "B-"
62+ } if ( gpa >= 77 ) {
63+ return "C+"
64+ } if ( gpa >= 73 ) {
65+ return "C"
66+ } if ( gpa >= 70 ) {
67+ return "C-" ;
68+ } if ( gpa >= 67 ) {
69+ return "D+" ;
70+ } if ( gpa >= 65 ) {
71+ return "D-" ;
72+ } else {
73+ return "F"
74+ }
4175}
4276
4377/**
@@ -47,7 +81,11 @@ export function convertGpaToLetterGrade(gpa: number): string {
4781 * @return The factorial of n.
4882 */
4983export function computeFactorial ( n : number ) : number {
50- return 0 ;
84+ let totalValue = 1
85+ for ( let i = 1 ; n >= i ; i ++ ) {
86+ totalValue *= i
87+ }
88+ return totalValue ;
5189}
5290
5391/**
@@ -57,7 +95,11 @@ export function computeFactorial(n: number): number {
5795 * @return The sum of all the values.
5896 */
5997export function addNumbers ( values : number [ ] ) : number {
60- return 0 ;
98+ let sum = 0
99+ for ( let i = 0 ; i < values . length ; i ++ ) {
100+ sum += values [ i ] ;
101+ }
102+ return sum ;
61103}
62104
63105/**
@@ -67,7 +109,17 @@ export function addNumbers(values: number[]): number {
67109 * @return An array containing the first `n` Fibonacci values.
68110 */
69111export function getFirstNFibonacciNumbers ( n : number ) : number [ ] {
70- return [ ] ;
112+ let array = [ n ]
113+
114+ if ( n <= 0 ) {
115+ return array ;
116+ }
117+
118+ for ( let i = 2 ; i < n ; i ++ ) {
119+ let cont = array [ i - 1 ] + array [ i - 2 ] ;
120+ array . push ( cont )
121+ }
122+ return array ;
71123}
72124
73125/**
@@ -93,10 +145,15 @@ export function binarySearch(
93145 const pivotIndex = Math . floor ( ( start + end ) / 2 ) ; // The index in the middle of the array.
94146
95147 // TODO(you): Finish implementing this algorithm
96-
148+ if ( values [ pivotIndex ] === value ) {
149+ return pivotIndex ;
150+ } if ( values [ pivotIndex ] < value ) {
151+ return binarySearch ( values , start , pivotIndex - 1 , value ) ;
152+ } else {
153+ return binarySearch ( values , pivotIndex - 1 , end , value ) ;
154+ }
97155 // If values[pivotIndex] is equal to value then return `pivotIndex`.
98156 // Else if values[pivotIndex] is greater than the value, then
99157 // call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
100158 // Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
101- return - 1 ;
102159}
0 commit comments