Skip to content

Commit 3ab563d

Browse files
committed
feat: added lesson_07
1 parent 894c36c commit 3ab563d

File tree

3 files changed

+37
-91
lines changed

3 files changed

+37
-91
lines changed

lesson_07/conditionals/Chutt_lesson07

Lines changed: 0 additions & 70 deletions
This file was deleted.

lesson_07/conditionals/src/lesson7.ts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ 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-
// TODO(you): Finish this method.
16-
17-
return 0;
14+
return distance;
1815
}
1916

2017
/**
@@ -24,7 +21,15 @@ export function compareStrings(a: string, b: string): number {
2421
* @return The factorial of n.
2522
*/
2623
export function computeFactorial(n: number): number {
27-
return 0;
24+
if (n < 0) {
25+
return 0;
26+
} else {
27+
let result = 1;
28+
for (let i = 2; i <= n; i++) {
29+
result = result * i;
30+
}
31+
return result;
32+
}
2833
}
2934

3035
/**
@@ -34,7 +39,20 @@ export function computeFactorial(n: number): number {
3439
* @return An array containing the first `n` Fibonacci values.
3540
*/
3641
export function getFirstNFibonacciNumbers(n: number): number[] {
37-
return [];
42+
if (n < 1) {
43+
return [];
44+
}
45+
if (n === 1) {
46+
return [1];
47+
}
48+
if (n === 2) {
49+
return [1, 1];
50+
}
51+
const sequence = [1, 1];
52+
for (let i = 2; i < n; ++i) {
53+
sequence[i] = sequence[i - 1] + sequence[i - 2];
54+
}
55+
return sequence;
3856
}
3957

4058
/**
@@ -59,7 +77,13 @@ export function binarySearch(
5977

6078
const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.
6179

62-
// TODO(you): Finish implementing this algorithm
80+
if (values[pivotIndex] === value) {
81+
return pivotIndex;
82+
} else if (values[pivotIndex] < value) {
83+
return binarySearch(values, pivotIndex + 1, end, value);
84+
} else {
85+
return binarySearch(values, start, pivotIndex - 1, value);
86+
}
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: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,28 @@
55
* @returns
66
*/
77
export function isLeapYear(year: number): boolean {
8-
// To determine a leap year, it has to be divisible by 4.
9-
if (year % 4 === 0) {
8+
if (year % 400 === 0) {
109
return true;
11-
}
12-
13-
if (year % 100 === 0) {
10+
} else if (year % 100 === 0) {
1411
return false;
15-
}
16-
17-
if (year % 400 === 0) {
12+
} else if (year % 4 === 0) {
1813
return true;
14+
} else {
15+
return false;
1916
}
20-
21-
return false;
2217
}
2318

2419
/**
2520
* Returns whether the given number is even or odd.
26-
*
2721
* @param num
2822
* @returns
2923
*/
3024
// To determine if anumber is even is has to be divisible by 2 with no remainder.
3125
// To determine if a number is odd, it will not be divisible by 2 evenly.
3226
export function isEvenOrOdd(num: number): string {
33-
return "";
3427
if (num % 2 === 0) {
3528
return "even";
36-
}
37-
if (num % 3 === 0) {
29+
} else {
3830
return "odd";
3931
}
4032
}

0 commit comments

Comments
 (0)