Skip to content

Commit f54640d

Browse files
author
Ezra Nyabuti
committed
feat: adds missing functions on lesson7 and part_d
1 parent a4c4623 commit f54640d

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

lesson_07/conditionals/.env.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
HW_VERSION=your homework version here
1+
HW_VERSION=D

lesson_07/conditionals/src/lesson7.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ export function compareStrings(a: string, b: string): number {
1111
// The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
1212
// if it is greater, and 0 if the strings are equal.
1313
const distance = computeLexicographicDistance(a, b);
14+
{
15+
if (distance < 0) {
16+
return -1;
17+
} else if (distance > 0) {
18+
return 1;
19+
} else {
20+
return 0;
21+
}
22+
}
1423

1524
// TODO(you): Finish this method.
1625

@@ -24,7 +33,15 @@ export function compareStrings(a: string, b: string): number {
2433
* @return The factorial of n.
2534
*/
2635
export function computeFactorial(n: number): number {
27-
return 0;
36+
if (n === 0) {
37+
return 1;
38+
} else if (n < 0) {
39+
return 0;
40+
} else if (n === 1) {
41+
return 1;
42+
} else {
43+
return n * computeFactorial(n - 1);
44+
}
2845
}
2946

3047
/**
@@ -34,7 +51,15 @@ export function computeFactorial(n: number): number {
3451
* @return An array containing the first `n` Fibonacci values.
3552
*/
3653
export function getFirstNFibonacciNumbers(n: number): number[] {
37-
return [];
54+
if (n <= 0) return [];
55+
if (n === 1) return [1];
56+
if (n === 2) return [1, 1];
57+
58+
const fibonacciNumbers = [1, 1];
59+
for (let i = 2; i < n; i++) {
60+
fibonacciNumbers.push(fibonacciNumbers[i - 1] + fibonacciNumbers[i - 2]);
61+
}
62+
return fibonacciNumbers;
3863
}
3964

4065
/**
@@ -60,7 +85,13 @@ export function binarySearch(
6085
const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.
6186

6287
// TODO(you): Finish implementing this algorithm
63-
88+
if (values[pivotIndex] === value) {
89+
return pivotIndex;
90+
} else if (values[pivotIndex] > value) {
91+
return binarySearch(values, start, pivotIndex - 1, value);
92+
} else {
93+
return binarySearch(values, pivotIndex + 1, end, value);
94+
}
6495
// If values[pivotIndex] is equal to value then return `pivotIndex`.
6596
// Else if values[pivotIndex] is greater than the value, then
6697
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;

lesson_07/conditionals/src/part_d.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
* @returns
88
*/
99
export function isWithinRange(num: number, min: number, max: number): boolean {
10+
if (num >= min && num <= max) {
11+
return true;
12+
}
1013
return false;
1114
}
1215

@@ -19,7 +22,12 @@ export function isWithinRange(num: number, min: number, max: number): boolean {
1922
* @returns
2023
*/
2124
export function isValidTriangle(a: number, b: number, c: number): boolean {
22-
return false;
25+
switch (true) {
26+
case a + b > c && a + c > b && b + c > a:
27+
return true;
28+
default:
29+
return false;
30+
}
2331
}
2432

2533
/**
@@ -30,5 +38,17 @@ export function isValidTriangle(a: number, b: number, c: number): boolean {
3038
* @returns
3139
*/
3240
export function getSeason(month: number): string {
41+
if (isWithinRange(month, 1, 12)) {
42+
switch (true) {
43+
case isWithinRange(month, 3, 5):
44+
return "Spring";
45+
case isWithinRange(month, 6, 8):
46+
return "Summer";
47+
case isWithinRange(month, 9, 11):
48+
return "Fall";
49+
default:
50+
return "Winter";
51+
}
52+
}
3353
return "Invalid month";
3454
}

0 commit comments

Comments
 (0)