Skip to content

Commit 8ab6ff9

Browse files
committed
feat: added lesson_07 work to lesson_07 branch
1 parent 68305cb commit 8ab6ff9

File tree

1 file changed

+70
-9
lines changed

1 file changed

+70
-9
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 70 additions & 9 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

@@ -15,16 +18,22 @@ export function canVote(age: number): boolean {
1518
*
1619
* @param a The first `string` to compare.
1720
* @param b The second `string` to compare.
18-
* @return -1 if a is less than b, 1 if a is greater than b, and 0 otherwise.
21+
* @return -1 if a (the first 'string) is less than b (the second 'string'), 1 if a is greater than b, and 0 otherwise.
1922
*/
2023
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.
24+
// The distance will be a number less than 0 if string `a` is lexicographically less than `b`,
25+
// 1 if it is greater b, and 0 if both strings are equal.
2326
const distance = computeLexicographicDistance(a, b);
2427

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

27-
return 0;
36+
// TODO(you): Finish this method.
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+
if (gpa === 4.0) {
50+
return "A";
51+
} else if (gpa > 4.0) {
52+
return "A";
53+
} else if (gpa <= 3.99 && gpa >= 3.7) {
54+
return "A-";
55+
} else if (gpa <= 3.69 && gpa >= 3.3) {
56+
return "B+";
57+
} else if (gpa <= 3.29 && gpa >= 3.0) {
58+
return "B";
59+
} else if (gpa <= 2.99 && gpa >= 2.7) {
60+
return "B-";
61+
} else if (gpa <= 2.69 && gpa >= 2.3) {
62+
return "C+";
63+
} else if (gpa <= 2.29 && gpa >= 2.0) {
64+
return "C";
65+
} else if (gpa <= 1.99 && gpa >= 1.7) {
66+
return "C-";
67+
} else if (gpa <= 1.69 && gpa >= 1.3) {
68+
return "D+";
69+
} else if (gpa <= 1.29 && gpa >= 1.0) {
70+
return "D";
71+
} else {
72+
return "F";
73+
}
4174
}
4275

4376
/**
@@ -47,7 +80,12 @@ 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 = 1; 1 <= n; i++) {
85+
product *= i;
86+
}
87+
88+
return product;
5189
}
5290

5391
/**
@@ -57,7 +95,11 @@ export function computeFactorial(n: number): number {
5795
* @return The sum of all the values.
5896
*/
5997
export function addNumbers(values: number[]): number {
60-
return 0;
98+
let sum = 0;
99+
for (const value of values) {
100+
sum += value;
101+
}
102+
return sum;
61103
}
62104

63105
/**
@@ -67,7 +109,18 @@ export function addNumbers(values: number[]): number {
67109
* @return An array containing the first `n` Fibonacci values.
68110
*/
69111
export function getFirstNFibonacciNumbers(n: number): number[] {
70-
return [];
112+
let current = 1;
113+
let prev = 0;
114+
115+
const numbers = [];
116+
for (let i = 1; i <= n; i++) {
117+
numbers.push(current);
118+
const nextNum = current + prev;
119+
prev = current;
120+
current = nextNum;
121+
}
122+
123+
return numbers;
71124
}
72125

73126
/**
@@ -91,12 +144,20 @@ export function binarySearch(
91144
}
92145

93146
const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.
147+
if (values[pivotIndex] === value) {
148+
return pivotIndex;
149+
} else if (values[pivotIndex] > value) {
150+
return binarySearch(values, start, pivotIndex - 1, value);
151+
} else {
152+
return binarySearch(values, pivotIndex + 1, end, value);
153+
}
94154

95155
// TODO(you): Finish implementing this algorithm
96156

97157
// If values[pivotIndex] is equal to value then return `pivotIndex`.
98158
// Else if values[pivotIndex] is greater than the value, then
99159
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
100160
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
161+
101162
return -1;
102163
}

0 commit comments

Comments
 (0)