Skip to content

Commit 6952d51

Browse files
committed
feat: created new branch and completed lesson
1 parent 89614e9 commit 6952d51

File tree

3 files changed

+74
-13
lines changed

3 files changed

+74
-13
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: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ 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+
return distance;
15+
}
1416

15-
// TODO(you): Finish this method.
17+
// TODO(you): Finish this method.
1618

17-
return 0;
18-
}
19+
// Removed misplaced return statement
1920

2021
/**
2122
* Computes the factorial of the given value of `n`.
@@ -24,7 +25,20 @@ export function compareStrings(a: string, b: string): number {
2425
* @return The factorial of n.
2526
*/
2627
export function computeFactorial(n: number): number {
27-
return 0;
28+
if (n < 0) {
29+
return 0;
30+
}
31+
if (n === 1) {
32+
return 1;
33+
}
34+
if (n === 2) {
35+
return 2;
36+
}
37+
let result = 1;
38+
for (let i = 2; i <= n; ++i) {
39+
result *= i;
40+
}
41+
return result;
2842
}
2943

3044
/**
@@ -34,7 +48,20 @@ export function computeFactorial(n: number): number {
3448
* @return An array containing the first `n` Fibonacci values.
3549
*/
3650
export function getFirstNFibonacciNumbers(n: number): number[] {
37-
return [];
51+
if (n < 1) {
52+
return [];
53+
}
54+
if (n === 1) {
55+
return [1];
56+
}
57+
if (n === 2) {
58+
return [1, 1];
59+
}
60+
const sequence = [1, 1];
61+
for (let i = 2; i < n; ++i) {
62+
sequence[i] = sequence[i - 1] + sequence[i - 2];
63+
}
64+
return sequence;
3865
}
3966

4067
/**
@@ -56,14 +83,19 @@ export function binarySearch(
5683
// The range is not valid so just return -1.
5784
return -1;
5885
}
59-
60-
const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.
61-
6286
// TODO(you): Finish implementing this algorithm
6387

6488
// If values[pivotIndex] is equal to value then return `pivotIndex`.
6589
// Else if values[pivotIndex] is greater than the value, then
6690
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
6791
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
68-
return -1;
92+
93+
const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.
94+
if (values[pivotIndex] === value) {
95+
return pivotIndex;
96+
} else if (values[pivotIndex] > value) {
97+
return binarySearch(values, start, pivotIndex - 1, value);
98+
} else {
99+
return binarySearch(values, pivotIndex + 1, end, value);
100+
}
69101
}

lesson_07/conditionals/src/part_b.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@
55
* @returns
66
*/
77
export function isLeapYear(year: number): boolean {
8-
return false;
8+
if (year % 400 === 0) {
9+
return true;
10+
} else if (year % 100 === 0) {
11+
return false;
12+
} else if (year % 4 === 0) {
13+
return true;
14+
} else {
15+
return false;
16+
}
917
}
1018

1119
/**
@@ -15,7 +23,11 @@ export function isLeapYear(year: number): boolean {
1523
* @returns
1624
*/
1725
export function isEvenOrOdd(num: number): string {
18-
return "";
26+
if (num % 2 === 0) {
27+
return "even";
28+
} else {
29+
return "odd";
30+
}
1931
}
2032

2133
/**
@@ -25,5 +37,22 @@ export function isEvenOrOdd(num: number): string {
2537
* @returns
2638
*/
2739
export function hasVowel(word: string): boolean {
28-
return false;
40+
const lowerCaseword = word.toLowerCase();
41+
if (lowerCaseword.includes("a")) {
42+
return true;
43+
}
44+
if (lowerCaseword.includes("e")) {
45+
return true;
46+
}
47+
if (lowerCaseword.includes("i")) {
48+
return true;
49+
}
50+
if (lowerCaseword.includes("o")) {
51+
return true;
52+
}
53+
if (lowerCaseword.includes("u")) {
54+
return true;
55+
} else {
56+
return false;
57+
}
2958
}

0 commit comments

Comments
 (0)