Skip to content

Commit 72adf41

Browse files
committed
fix: completed the functions
1 parent 70dccf2 commit 72adf41

File tree

1 file changed

+63
-8
lines changed

1 file changed

+63
-8
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import { computeLexicographicDistance } from "./util.js";
77
* @return True if the age corresponds to a voting age and false otherwise.
88
*/
99
export function canVote(age: number): boolean {
10+
if (age >= 18) {
11+
return true;
12+
}
1013
return false;
1114
}
1215

@@ -22,9 +25,14 @@ export function compareStrings(a: string, b: string): number {
2225
// if it is greater, and 0 if the strings are equal.
2326
const distance = computeLexicographicDistance(a, b);
2427

28+
if (distance < 0) {
29+
return -1;
30+
} else if (distance > 0) {
31+
return 1;
32+
} else {
33+
return 0;
34+
}
2535
// TODO(you): Finish this method.
26-
27-
return 0;
2836
}
2937

3038
/**
@@ -37,7 +45,29 @@ export function compareStrings(a: string, b: string): number {
3745
* @return The letter grade ("A+", "A", "A-", "B+", etc.).
3846
*/
3947
export function convertGpaToLetterGrade(gpa: number): string {
40-
return "F";
48+
if (gpa == 4.0) {
49+
return "A";
50+
} else if (gpa <= 3.99 && gpa >= 3.7) {
51+
return "A-";
52+
} else if (gpa <= 3.69 && gpa >= 3.3) {
53+
return "B+";
54+
} else if (gpa <= 3.29 && gpa >= 3.0) {
55+
return "B";
56+
} else if (gpa <= 2.99 && gpa >= 2.7) {
57+
return "B-";
58+
} else if (gpa <= 2.69 && gpa >= 2.3) {
59+
return "C+";
60+
} else if (gpa <= 2.29 && gpa >= 2.0) {
61+
return "C";
62+
} else if (gpa <= 1.99 && gpa >= 1.7) {
63+
return "C-";
64+
} else if (gpa <= 1.69 && gpa >= 1.3) {
65+
return "D+";
66+
} else if (gpa <= 1.29 && gpa >= 1.0) {
67+
return "D";
68+
} else {
69+
return "F";
70+
}
4171
}
4272

4373
/**
@@ -47,17 +77,26 @@ export function convertGpaToLetterGrade(gpa: number): string {
4777
* @return The factorial of n.
4878
*/
4979
export function computeFactorial(n: number): number {
50-
return 0;
80+
if (n == 0 || n == 1) {
81+
return 1;
82+
} else if (n > 1) {
83+
return n * computeFactorial(n - 1);
84+
}
85+
return n;
5186
}
52-
87+
/* 4! = 4 x 3 x 2 x 1 = 24*/
5388
/**
5489
* Adds all of the provided values and returns the sum.
5590
*
5691
* @param values The values to sum.
5792
* @return The sum of all the values.
5893
*/
5994
export function addNumbers(values: number[]): number {
60-
return 0;
95+
let sum = 0;
96+
for (const value of values) {
97+
sum += value;
98+
}
99+
return sum;
61100
}
62101

63102
/**
@@ -67,7 +106,17 @@ export function addNumbers(values: number[]): number {
67106
* @return An array containing the first `n` Fibonacci values.
68107
*/
69108
export function getFirstNFibonacciNumbers(n: number): number[] {
70-
return [];
109+
const fibArray: number[] = [];
110+
let a = 0;
111+
let b = 1;
112+
let c: number;
113+
while (fibArray.length < n) {
114+
c = a;
115+
a = b;
116+
b = c + b;
117+
fibArray.push(a);
118+
}
119+
return fibArray;
71120
}
72121

73122
/**
@@ -94,9 +143,15 @@ export function binarySearch(
94143

95144
// TODO(you): Finish implementing this algorithm
96145

146+
if (values[pivotIndex] == value) {
147+
return pivotIndex;
148+
} else if (values[pivotIndex] > value) {
149+
return binarySearch(values, start, pivotIndex - 1, value);
150+
} else {
151+
return binarySearch(values, pivotIndex + 1, end, value);
152+
}
97153
// If values[pivotIndex] is equal to value then return `pivotIndex`.
98154
// Else if values[pivotIndex] is greater than the value, then
99155
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
100156
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
101-
return -1;
102157
}

0 commit comments

Comments
 (0)