Skip to content

Commit c56e8a3

Browse files
committed
feat/finished lesson_07 homework
1 parent a4c4623 commit c56e8a3

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-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=B

lesson_07/conditionals/src/lesson7.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ 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+
}
20+
}
1421

1522
// TODO(you): Finish this method.
1623

@@ -24,7 +31,9 @@ export function compareStrings(a: string, b: string): number {
2431
* @return The factorial of n.
2532
*/
2633
export function computeFactorial(n: number): number {
27-
return 0;
34+
if (n === 0) return 1;
35+
else if (n < 0) return 0;
36+
else return n * computeFactorial(n - 1);
2837
}
2938

3039
/**
@@ -34,7 +43,15 @@ export function computeFactorial(n: number): number {
3443
* @return An array containing the first `n` Fibonacci values.
3544
*/
3645
export function getFirstNFibonacciNumbers(n: number): number[] {
37-
return [];
46+
if (n <= 0) return [];
47+
if (n === 1) return [1];
48+
if (n === 2) return [1, 1];
49+
50+
const FibonacciNumbers = [1, 1];
51+
for (let i = 2; i < n; i++) {
52+
FibonacciNumbers.push(FibonacciNumbers[i - 1] + FibonacciNumbers[i - 2]);
53+
}
54+
return FibonacciNumbers;
3855
}
3956

4057
/**
@@ -60,6 +77,13 @@ export function binarySearch(
6077
const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.
6178

6279
// TODO(you): Finish implementing this algorithm
80+
if (values[pivotIndex] === value) {
81+
return pivotIndex;
82+
}
83+
if (values[pivotIndex] > value) {
84+
return binarySearch(values, start, pivotIndex - 1, value);
85+
}
86+
return binarySearch(values, pivotIndex + 1, end, value);
6387

6488
// If values[pivotIndex] is equal to value then return `pivotIndex`.
6589
// Else if values[pivotIndex] is greater than the value, then

lesson_07/conditionals/src/part_b.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,30 @@
55
* @returns
66
*/
77
export function isLeapYear(year: number): boolean {
8-
return false;
8+
switch (true) {
9+
case year % 400 === 0:
10+
return true;
11+
case year % 100 === 0:
12+
return false;
13+
case year % 4 === 0:
14+
return true;
15+
default:
16+
return false;
17+
}
918
}
10-
1119
/**
1220
* Returns whether the given number is even or odd.
1321
*
1422
* @param num
1523
* @returns
1624
*/
1725
export function isEvenOrOdd(num: number): string {
26+
if (num % 2 === 0) {
27+
return "even";
28+
}
29+
if (num % 2 !== 0) {
30+
return "odd";
31+
}
1832
return "";
1933
}
2034

@@ -25,5 +39,8 @@ export function isEvenOrOdd(num: number): string {
2539
* @returns
2640
*/
2741
export function hasVowel(word: string): boolean {
42+
if (word.match(/[aeiou]/i)) {
43+
return true;
44+
}
2845
return false;
2946
}

0 commit comments

Comments
 (0)