Skip to content

Commit b62bbc4

Browse files
thusser252anthonydmays
authored andcommitted
Completed lesson07 exercises: implemented compareStrings, computeFactorial, getFirstNFibonacciNumbers, binarySearch, and other conditional functions
1 parent 0a69ac4 commit b62bbc4

File tree

2 files changed

+93
-18
lines changed

2 files changed

+93
-18
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ export function compareStrings(a: string, b: string): number {
1212
// if it is greater, and 0 if the strings are equal.
1313
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
}
1923

2024
/**
@@ -24,7 +28,10 @@ export function compareStrings(a: string, b: string): number {
2428
* @return The factorial of n.
2529
*/
2630
export function computeFactorial(n: number): number {
27-
return 0;
31+
if (n <= 1) {
32+
return 1;
33+
}
34+
return n * computeFactorial(n - 1);
2835
}
2936

3037
/**
@@ -34,7 +41,21 @@ export function computeFactorial(n: number): number {
3441
* @return An array containing the first `n` Fibonacci values.
3542
*/
3643
export function getFirstNFibonacciNumbers(n: number): number[] {
37-
return [];
44+
if (n <= 0) {
45+
return [];
46+
}
47+
48+
const fibonacci: number[] = [1];
49+
if (n === 1) {
50+
return fibonacci;
51+
}
52+
53+
fibonacci.push(1);
54+
for (let i = 2; i < n; i++) {
55+
fibonacci.push(fibonacci[i - 1] + fibonacci[i - 2]);
56+
}
57+
58+
return fibonacci;
3859
}
3960

4061
/**
@@ -59,11 +80,11 @@ export function binarySearch(
5980

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

62-
// TODO(you): Finish implementing this algorithm
63-
64-
// If values[pivotIndex] is equal to value then return `pivotIndex`.
65-
// Else if values[pivotIndex] is greater than the value, then
66-
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
67-
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
68-
return -1;
83+
if (values[pivotIndex] === value) {
84+
return pivotIndex;
85+
} else if (values[pivotIndex] > value) {
86+
return binarySearch(values, start, pivotIndex - 1, value);
87+
} else {
88+
return binarySearch(values, pivotIndex + 1, end, value);
89+
}
6990
}

lesson_07/conditionals/src/part_c.ts

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,64 @@
33
* and at least one uppercase letter and one digit.
44
*
55
* @param password
6-
* @returns
6+
* @returns true if strong, false otherwise
77
*/
88
export function isStrongPassword(password: string): boolean {
9-
return false;
9+
// Check length
10+
if (password.length < 8) {
11+
return false;
12+
}
13+
14+
let hasUppercase = false;
15+
let hasDigit = false;
16+
17+
// Check for uppercase and digits in one pass through the string
18+
for (let i = 0; i < password.length; i++) {
19+
const ch = password[i];
20+
21+
// Check uppercase
22+
if (ch >= "A" && ch <= "Z") {
23+
hasUppercase = true;
24+
}
25+
26+
// Check digit
27+
if (ch >= "0" && ch <= "9") {
28+
hasDigit = true;
29+
}
30+
}
31+
32+
// Strong only if all conditions are met
33+
if (hasUppercase && hasDigit) {
34+
return true;
35+
} else {
36+
return false;
37+
}
1038
}
1139

1240
/**
1341
* Determines the day of the week on the given 0-based number.
1442
*
1543
* @param day
16-
* @returns
44+
* @returns The day name or "Invalid day" if out of range.
1745
*/
1846
export function getDayOfWeek(day: number): string {
19-
return "";
47+
if (day === 0) {
48+
return "Sunday";
49+
} else if (day === 1) {
50+
return "Monday";
51+
} else if (day === 2) {
52+
return "Tuesday";
53+
} else if (day === 3) {
54+
return "Wednesday";
55+
} else if (day === 4) {
56+
return "Thursday";
57+
} else if (day === 5) {
58+
return "Friday";
59+
} else if (day === 6) {
60+
return "Saturday";
61+
} else {
62+
return "";
63+
}
2064
}
2165

2266
/**
@@ -28,8 +72,18 @@ export function getDayOfWeek(day: number): string {
2872
* - Seniors 60 years old and older pay 15 dollars.
2973
*
3074
* @param age
31-
* @returns
75+
* @returns Ticket price in dollars.
3276
*/
3377
export function getTicketPrice(age: number): number {
34-
return 0;
78+
if (age < 5) {
79+
return 0;
80+
} else if (age >= 5 && age <= 17) {
81+
return 10;
82+
} else if (age >= 18 && age <= 59) {
83+
return 20;
84+
} else if (age >= 60) {
85+
return 15;
86+
} else {
87+
return 0;
88+
}
3589
}

0 commit comments

Comments
 (0)