Skip to content

Commit 7f332cf

Browse files
committed
chore added functions into lesson 7 and part e
1 parent 168ca99 commit 7f332cf

File tree

2 files changed

+60
-11
lines changed

2 files changed

+60
-11
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,33 @@ import { computeLexicographicDistance } from "./util.js";
1010
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.
13-
const distance = computeLexicographicDistance(a, b);
13+
const distance = computeLexicographicDistance(a, b);
1414

15-
// TODO(you): Finish this method.
16-
17-
return 0;
15+
if (distance < 0) {
16+
return -1;
17+
} else if (distance > 0) {
18+
return 1;
19+
} else {
20+
return 0;
21+
}
1822
}
19-
2023
/**
2124
* Computes the factorial of the given value of `n`.
2225
*
2326
* @param n The value for which to compute the factorial.
2427
* @return The factorial of n.
2528
*/
2629
export function computeFactorial(n: number): number {
27-
return 0;
30+
if (n === 0 ) {
31+
return 1; // Base case: 0! and 1! both equal 1
32+
}
33+
else if (n === 1) {
34+
return 1;
35+
}
36+
else if (n < 0) {
37+
return 0;
38+
}
39+
return n * computeFactorial(n - 1);
2840
}
2941

3042
/**
@@ -34,6 +46,22 @@ export function computeFactorial(n: number): number {
3446
* @return An array containing the first `n` Fibonacci values.
3547
*/
3648
export function getFirstNFibonacciNumbers(n: number): number[] {
49+
for (let i = 0; i < n; i++) {
50+
if (n === 0) {
51+
return [];
52+
}
53+
else if (n === 1) {
54+
return [1];
55+
}
56+
else if (n === 2) {
57+
return [1, 1];
58+
}
59+
else {
60+
const fib = getFirstNFibonacciNumbers(n - 1);
61+
fib.push(fib[fib.length - 1] + fib[fib.length - 2]);
62+
return fib;
63+
}
64+
}
3765
return [];
3866
}
3967

@@ -57,9 +85,19 @@ export function binarySearch(
5785
return -1;
5886
}
5987

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

62-
// TODO(you): Finish implementing this algorithm
89+
const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.
90+
if (values[pivotIndex] === value) {
91+
return pivotIndex;
92+
} else if (values[pivotIndex] > value) {
93+
return binarySearch(values, start, pivotIndex - 1, value);
94+
} else {
95+
return binarySearch(values, pivotIndex + 1, end, value);
96+
return -1;
97+
}
98+
99+
100+
63101

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

lesson_07/conditionals/src/part_e.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
* Write a function that takes a single character as an argument and
33
* returns boolean value true if the character is an uppercase letter.
44
*
5-
* @param char
6-
* @returns
5+
*
6+
*
77
*/
88
export function isUppercase(char: string): boolean {
9+
if (char >= "A" && char <= "Z") {
10+
return true;
11+
}
12+
913
return false;
1014
}
1115

@@ -17,6 +21,9 @@ export function isUppercase(char: string): boolean {
1721
* @returns
1822
*/
1923
export function canGetDriverLicense(age: number, passedTest: boolean): boolean {
24+
if (age >= 18 && passedTest) {
25+
return true;
26+
}
2027
return false;
2128
}
2229

@@ -29,5 +36,9 @@ export function canGetDriverLicense(age: number, passedTest: boolean): boolean {
2936
* @returns
3037
*/
3138
export function isStoreOpen(day: string, hour: number): boolean {
32-
return false;
39+
if (day === "Sunday" || hour < 9 || hour >= 21) {
40+
return false;
41+
} else {
42+
return true;
43+
}
3344
}

0 commit comments

Comments
 (0)