Skip to content

Commit 92e7a05

Browse files
committed
feat: adds Shawn Dunsmore Lesson07
1 parent 265a107 commit 92e7a05

File tree

1 file changed

+65
-22
lines changed

1 file changed

+65
-22
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 65 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,25 @@ export function compareStrings(a: string, b: string): number {
4343
* @return The letter grade ("A+", "A", "A-", "B+", etc.).
4444
*/
4545
export function convertGpaToLetterGrade(gpa: number): string {
46-
if (gpa > 4.0) return "A+";
47-
if (gpa >= 3.7) return "A";
48-
if (gpa >= 3.3) return "A-";
49-
if (gpa >= 3.0) return "B+";
50-
if (gpa >= 2.7) return "B";
51-
if (gpa >= 2.3) return "B-";
52-
if (gpa >= 2.0) return "C+";
53-
if (gpa >= 1.7) return "C";
54-
if (gpa >= 1.3) return "C-";
55-
if (gpa >= 1.0) return "D+";
56-
if (gpa >= 0.0) return "D";
57-
46+
if (gpa >= 4.0) return "A";
47+
if (gpa >= 3.99) return "A-";
48+
if (gpa >= 3.7) return "A-";
49+
if (gpa >= 3.69) return "B+";
50+
if (gpa >= 3.3) return "B+";
51+
if (gpa >= 3.29) return "B";
52+
if (gpa >= 3.0) return "B";
53+
if (gpa >= 2.99) return "B-";
54+
if (gpa >= 2.7) return "B-";
55+
if (gpa >= 2.69) return "C+";
56+
if (gpa >= 2.3) return "C+";
57+
if (gpa >= 2.29) return "C";
58+
if (gpa >= 2.0) return "C";
59+
if (gpa >= 1.99) return "C-";
60+
if (gpa >= 1.7) return "C-";
61+
if (gpa >= 1.69) return "D+";
62+
if (gpa >= 1.3) return "D+";
63+
if (gpa >= 1.29) return "D";
64+
if (gpa >= 1.0) return "D";
5865
return "F";
5966
}
6067

@@ -96,7 +103,23 @@ export function addNumbers(values: number[]): number {
96103
* @return An array containing the first `n` Fibonacci values.
97104
*/
98105
export function getFirstNFibonacciNumbers(n: number): number[] {
99-
return [];
106+
const fibNumbers: number[] = [];
107+
108+
if (n <= 0) return fibNumbers;
109+
110+
let first = 1,
111+
second = 1;
112+
fibNumbers.push(first);
113+
if (n > 1) fibNumbers.push(second);
114+
115+
for (let i = 2; i < n; i++) {
116+
const next = first + second;
117+
fibNumbers.push(next);
118+
first = second;
119+
second = next;
120+
}
121+
122+
return fibNumbers;
100123
}
101124

102125
/**
@@ -112,21 +135,41 @@ export function binarySearch(
112135
values: number[],
113136
start: number,
114137
end: number,
115-
value: number,
138+
target: number,
116139
): number {
117-
if (end < start) {
118-
// The range is not valid so just return -1.
119-
140+
if (start > end) {
120141
return -1;
121142
}
122143

144+
const mid = Math.floor((start + end) / 2);
145+
146+
if (values[mid] === target) {
147+
return mid;
148+
} else if (values[mid] > target) {
149+
return binarySearch(values, start, mid - 1, target);
150+
} else {
151+
return binarySearch(values, mid + 1, end, target);
152+
}
153+
154+
/**
155+
* The following code is part of the binary search logic:
156+
*/
157+
123158
const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.
124159

125-
// TODO(you): Finish implementing this algorithm
160+
if (values[pivotIndex] === target) {
161+
return pivotIndex;
162+
} else if (values[pivotIndex] > target) {
163+
return binarySearch(values, start, pivotIndex - 1, target);
164+
} else {
165+
return binarySearch(values, pivotIndex + 1, end, target);
166+
}
126167

127-
// If values[pivotIndex] is equal to value then return `pivotIndex`.
128-
// Else if values[pivotIndex] is greater than the value, then
129-
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
130-
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
131168
return -1;
132169
}
170+
// TODO(you): Finish implementing this algorithm
171+
172+
// If values[pivotIndex] is equal to value then return `pivotIndex`.
173+
// Else if values[pivotIndex] is greater than the value, then
174+
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
175+
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.

0 commit comments

Comments
 (0)