Skip to content

Commit a7c2425

Browse files
committed
feat: David Smith Lesson 07 Final Commit
1 parent 1536bd9 commit a7c2425

File tree

1 file changed

+60
-22
lines changed

1 file changed

+60
-22
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 60 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,30 @@ 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-
return false;
10+
if (age >= 18) {
11+
return true;
12+
} else return false;
1113
}
1214

1315
/**
1416
* Compares two strings lexicographically.
1517
*
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.
1820
* @return -1 if a is less than b, 1 if a is greater than b, and 0 otherwise.
1921
*/
2022
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+
// The distance will be a number less than 0 if string a is lexicographically less than b,
24+
// greater than 0 if a is greater, and 0 if the strings are equal.
2325
const distance = computeLexicographicDistance(a, b);
2426

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

3036
/**
@@ -37,7 +43,27 @@ export function compareStrings(a: string, b: string): number {
3743
* @return The letter grade ("A+", "A", "A-", "B+", etc.).
3844
*/
3945
export function convertGpaToLetterGrade(gpa: number): string {
40-
return "F";
46+
if (gpa === 4) {
47+
return "A";
48+
} else if (gpa >= 3.7) {
49+
return "A-";
50+
} else if (gpa >= 3.3) {
51+
return "B+";
52+
} else if (gpa >= 3.0) {
53+
return "B";
54+
} else if (gpa >= 2.7) {
55+
return "B-";
56+
} else if (gpa >= 2.3) {
57+
return "C+";
58+
} else if (gpa >= 2.0) {
59+
return "C";
60+
} else if (gpa >= 1.7) {
61+
return "C-";
62+
} else if (gpa >= 1.3) {
63+
return "D+";
64+
} else if (gpa >= 1.0) {
65+
return "D";
66+
} else return "F";
4167
}
4268

4369
/**
@@ -47,7 +73,10 @@ export function convertGpaToLetterGrade(gpa: number): string {
4773
* @return The factorial of n.
4874
*/
4975
export function computeFactorial(n: number): number {
50-
return 0;
76+
if (n < 0) {
77+
throw new Error("Negative numbers are invalid.");
78+
}
79+
return n <= 1 ? 1 : n * computeFactorial(n - 1);
5180
}
5281

5382
/**
@@ -57,17 +86,27 @@ export function computeFactorial(n: number): number {
5786
* @return The sum of all the values.
5887
*/
5988
export function addNumbers(values: number[]): number {
60-
return 0;
89+
return values.reduce((sum, current) => sum + current, 0);
6190
}
6291

6392
/**
6493
* Returns an array of the first `n` Fibonacci numbers starting from 1.
6594
*
66-
* @param n The first `n` of Fibonacci values to compute.
95+
* @param n The first `n` Fibonacci values to compute.
6796
* @return An array containing the first `n` Fibonacci values.
6897
*/
6998
export function getFirstNFibonacciNumbers(n: number): number[] {
70-
return [];
99+
if (n <= 0) return [];
100+
if (n === 1) return [1];
101+
102+
const fibonacci: number[] = [1, 1];
103+
104+
for (let i = 2; i < n; i++) {
105+
const nextFibonacci = fibonacci[i - 1] + fibonacci[i - 2];
106+
fibonacci.push(nextFibonacci);
107+
}
108+
109+
return fibonacci;
71110
}
72111

73112
/**
@@ -90,13 +129,12 @@ export function binarySearch(
90129
return -1;
91130
}
92131

93-
const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.
94-
95-
// TODO(you): Finish implementing this algorithm
96-
97-
// If values[pivotIndex] is equal to value then return `pivotIndex`.
98-
// 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.
101-
return -1;
132+
const pivotIndex = Math.floor((start + end) / 2);
133+
if (values[pivotIndex] === value) {
134+
return pivotIndex;
135+
} else if (values[pivotIndex] > value) {
136+
return binarySearch(values, start, pivotIndex - 1, value);
137+
} else {
138+
return binarySearch(values, pivotIndex + 1, end, value);
139+
}
102140
}

0 commit comments

Comments
 (0)