Skip to content

Commit 86deb64

Browse files
committed
feat: adds branch
1 parent 888672a commit 86deb64

File tree

3 files changed

+83
-6
lines changed

3 files changed

+83
-6
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: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@ export function compareStrings(a: string, b: string): number {
1313
const distance = computeLexicographicDistance(a, b);
1414

1515
// TODO(you): Finish this method.
16-
16+
if (distance < 0) {
17+
return -1;
18+
} else if (distance > 0) {
19+
return 1;
20+
} else {
1721
return 0;
22+
}
1823
}
1924

2025
/**
@@ -24,7 +29,19 @@ export function compareStrings(a: string, b: string): number {
2429
* @return The factorial of n.
2530
*/
2631
export function computeFactorial(n: number): number {
27-
return 0;
32+
if (n === 0) {
33+
return 1; // The factorial of 0 is 1.
34+
}
35+
36+
if (n < 0) {
37+
return 0; // Factorial is not defined for negative numbers.
38+
}
39+
40+
let result = 1;
41+
for (let i = 1; i <= n; i++) {
42+
result *= i; // Multiply result by i for each i from 1 to n.
43+
}
44+
return result;
2845
}
2946

3047
/**
@@ -34,7 +51,28 @@ 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+
55+
56+
if (n === 0) {
57+
return []; // If n is 0, return an empty array.
58+
}
59+
60+
const fibonacciNumbers: number[] = [1]; // Initialize the array with the first Fibonacci number.
61+
62+
63+
if (n === 1) {
64+
return fibonacciNumbers; // If n is 1, return the array with the first Fibonacci number.
65+
}
66+
67+
fibonacciNumbers.push(1); // Add the second Fibonacci number (1) to the array.
68+
69+
70+
for (let i = 2; i < n; i++) {
71+
const nextFibonacciNumber =
72+
fibonacciNumbers[fibonacciNumbers.length - 1] + fibonacciNumbers[fibonacciNumbers.length - 2]; // The next Fibonacci number is the sum of the last two.
73+
fibonacciNumbers.push(nextFibonacciNumber); // Add the next Fibonacci number
74+
}
75+
return fibonacciNumbers; // Return the array of Fibonacci numbers.
3876
}
3977

4078
/**
@@ -60,10 +98,27 @@ export function binarySearch(
6098
const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.
6199

62100
// TODO(you): Finish implementing this algorithm
101+
if (values[pivotIndex] === value) {
102+
// If the value at pivotIndex is equal to the value we are looking for, return pivotIndex.
103+
return pivotIndex;
104+
}
105+
else if (values[pivotIndex] > value) {
106+
// If the value at pivotIndex is greater than the value we are looking for,
107+
// search the left half of the array.
108+
return binarySearch(values, start, pivotIndex - 1, value);
109+
}
110+
else {
111+
// If the value at pivotIndex is less than the value we are looking for,
112+
// search the right half of the array.
113+
return binarySearch(values, pivotIndex + 1, end, value);
114+
}
115+
116+
63117

64118
// If values[pivotIndex] is equal to value then return `pivotIndex`.
65119
// Else if values[pivotIndex] is greater than the value, then
66120
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
67121
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
68-
return -1;
122+
123+
return -1;
69124
}

lesson_07/conditionals/src/part_b.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,17 @@
55
* @returns
66
*/
77
export function isLeapYear(year: number): boolean {
8+
9+
if (year % 400 === 0) {
10+
return true;
11+
}
12+
if (year % 100 === 0) {
813
return false;
14+
}
15+
if (year % 4 === 0) {
16+
return true;
17+
}
18+
return false;
919
}
1020

1121
/**
@@ -15,7 +25,12 @@ export function isLeapYear(year: number): boolean {
1525
* @returns
1626
*/
1727
export function isEvenOrOdd(num: number): string {
18-
return "";
28+
if (num % 2 === 0) {
29+
return "even";
30+
}
31+
else {
32+
return "odd";
33+
}
1934
}
2035

2136
/**
@@ -25,5 +40,12 @@ export function isEvenOrOdd(num: number): string {
2540
* @returns
2641
*/
2742
export function hasVowel(word: string): boolean {
43+
word = word.toLowerCase();
44+
45+
if (word.includes('a') || word.includes("e") || word.includes("i") || word.includes("o") || word.includes("u")) {
46+
return true;
47+
}
48+
else {
2849
return false;
50+
}
2951
}

0 commit comments

Comments
 (0)