Skip to content

Commit 06048ed

Browse files
committed
feat: populated lesson 7 file with completed functions. all checks passed
1 parent 83d16b5 commit 06048ed

File tree

1 file changed

+67
-8
lines changed

1 file changed

+67
-8
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ 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 {
13+
return false;
14+
}
1115
}
1216

1317
/**
@@ -23,8 +27,13 @@ export function compareStrings(a: string, b: string): number {
2327
const distance = computeLexicographicDistance(a, b);
2428

2529
// TODO(you): Finish this method.
26-
27-
return 0;
30+
let distanceCompareNum = 0;
31+
if (distance < 0) {
32+
distanceCompareNum = -1;
33+
} else if (distance > 0) {
34+
distanceCompareNum = 1;
35+
}
36+
return distanceCompareNum;
2837
}
2938

3039
/**
@@ -37,7 +46,31 @@ export function compareStrings(a: string, b: string): number {
3746
* @return The letter grade ("A+", "A", "A-", "B+", etc.).
3847
*/
3948
export function convertGpaToLetterGrade(gpa: number): string {
40-
return "F";
49+
let letterGrade = "";
50+
if (gpa >= 4.0) {
51+
letterGrade = "A";
52+
} else if (gpa < 4.0 && gpa >= 3.7) {
53+
letterGrade = "A-";
54+
} else if (gpa <= 3.69 && gpa >= 3.3) {
55+
letterGrade = "B+";
56+
} else if (gpa <= 3.29 && gpa >= 3.0) {
57+
letterGrade = "B";
58+
} else if (gpa <= 2.99 && gpa >= 2.7) {
59+
letterGrade = "B-";
60+
} else if (gpa <= 2.69 && gpa >= 2.3) {
61+
letterGrade = "C+";
62+
} else if (gpa <= 2.29 && gpa >= 2.0) {
63+
letterGrade = "C";
64+
} else if (gpa <= 1.99 && gpa >= 1.7) {
65+
letterGrade = "C-";
66+
} else if (gpa <= 1.69 && gpa >= 1.3) {
67+
letterGrade = "D+";
68+
} else if (gpa <= 1.29 && gpa >= 1.0) {
69+
letterGrade = "D";
70+
} else if (gpa <= 0.99) {
71+
letterGrade = "F";
72+
}
73+
return letterGrade;
4174
}
4275

4376
/**
@@ -47,7 +80,11 @@ export function convertGpaToLetterGrade(gpa: number): string {
4780
* @return The factorial of n.
4881
*/
4982
export function computeFactorial(n: number): number {
50-
return 0;
83+
let product = 1;
84+
for (let i = n; i >= 1; i--) {
85+
product *= i;
86+
}
87+
return product;
5188
}
5289

5390
/**
@@ -57,7 +94,11 @@ export function computeFactorial(n: number): number {
5794
* @return The sum of all the values.
5895
*/
5996
export function addNumbers(values: number[]): number {
60-
return 0;
97+
let sum = 0;
98+
for (const i of values) {
99+
sum += i;
100+
}
101+
return sum;
61102
}
62103

63104
/**
@@ -67,7 +108,19 @@ export function addNumbers(values: number[]): number {
67108
* @return An array containing the first `n` Fibonacci values.
68109
*/
69110
export function getFirstNFibonacciNumbers(n: number): number[] {
70-
return [];
111+
const arr = [];
112+
if (n >= 1) {
113+
arr.push(1);
114+
}
115+
if (n >= 2) {
116+
arr.push(1);
117+
}
118+
if (n >= 3) {
119+
for (let i = 2; i < n; i++) {
120+
arr.push(arr[i - 1] + arr[i - 2]);
121+
}
122+
}
123+
return arr;
71124
}
72125

73126
/**
@@ -98,5 +151,11 @@ export function binarySearch(
98151
// Else if values[pivotIndex] is greater than the value, then
99152
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
100153
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
101-
return -1;
154+
if (values[pivotIndex] === value) {
155+
return pivotIndex;
156+
} else if (values[pivotIndex] > value) {
157+
return binarySearch(values, start, pivotIndex - 1, value);
158+
} else {
159+
return binarySearch(values, pivotIndex + 1, end, value);
160+
}
102161
}

0 commit comments

Comments
 (0)