Skip to content

Commit 8c9b39b

Browse files
chore: adds completed functions for lesson7 and part_c
1 parent a4c4623 commit 8c9b39b

File tree

2 files changed

+72
-9
lines changed

2 files changed

+72
-9
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ 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+
if (distance < 0) {
15+
return -1;
16+
} else if (distance > 0) {
17+
return 1;
18+
} else {
19+
return 0;
20+
}
1821
}
1922

2023
/**
@@ -24,7 +27,15 @@ export function compareStrings(a: string, b: string): number {
2427
* @return The factorial of n.
2528
*/
2629
export function computeFactorial(n: number): number {
27-
return 0;
30+
if (n < 0) {
31+
return 0;
32+
} else {
33+
let result = 1;
34+
for (let i = 2; i <= n; i++) {
35+
result = result * i;
36+
}
37+
return result;
38+
}
2839
}
2940

3041
/**
@@ -34,7 +45,16 @@ export function computeFactorial(n: number): number {
3445
* @return An array containing the first `n` Fibonacci values.
3546
*/
3647
export function getFirstNFibonacciNumbers(n: number): number[] {
37-
return [];
48+
if (n === 0) {
49+
return [];
50+
}
51+
const resultsArr: number[] = [1, 1];
52+
if (n > 1) {
53+
for (let i = 2; i < n; i++) {
54+
resultsArr.push(resultsArr[i - 1] + resultsArr[i - 2]);
55+
}
56+
}
57+
return resultsArr;
3858
}
3959

4060
/**
@@ -60,7 +80,13 @@ export function binarySearch(
6080
const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.
6181

6282
// TODO(you): Finish implementing this algorithm
63-
83+
if (values[pivotIndex] === value) {
84+
return pivotIndex;
85+
} else if (values[pivotIndex] < value) {
86+
return binarySearch(values, pivotIndex + 1, end, value);
87+
} else {
88+
return binarySearch(values, start, pivotIndex - 1, value);
89+
}
6490
// If values[pivotIndex] is equal to value then return `pivotIndex`.
6591
// Else if values[pivotIndex] is greater than the value, then
6692
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;

lesson_07/conditionals/src/part_c.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
* @returns
77
*/
88
export function isStrongPassword(password: string): boolean {
9+
if (password.length >= 8) {
10+
return /\d/.test(password) && /[A-Z]/.test(password);
11+
}
912
return false;
1013
}
1114

@@ -16,7 +19,33 @@ export function isStrongPassword(password: string): boolean {
1619
* @returns
1720
*/
1821
export function getDayOfWeek(day: number): string {
19-
return "";
22+
let result = "";
23+
switch (day) {
24+
case 0:
25+
result = "Sunday";
26+
break;
27+
case 1:
28+
result = "Monday";
29+
break;
30+
case 2:
31+
result = "Tuesday";
32+
break;
33+
case 3:
34+
result = "Wednesday";
35+
break;
36+
case 4:
37+
result = "Thursday";
38+
break;
39+
case 5:
40+
result = "Friday";
41+
break;
42+
case 6:
43+
result = "Saturday";
44+
break;
45+
default:
46+
result = "";
47+
}
48+
return result;
2049
}
2150

2251
/**
@@ -31,5 +60,13 @@ export function getDayOfWeek(day: number): string {
3160
* @returns
3261
*/
3362
export function getTicketPrice(age: number): number {
34-
return 0;
63+
if (age < 5) {
64+
return 0;
65+
} else if (age >= 5 && age <= 17) {
66+
return 10;
67+
} else if (age >= 18 && age <= 59) {
68+
return 20;
69+
} else {
70+
return 15;
71+
}
3572
}

0 commit comments

Comments
 (0)