Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lesson_07/conditionals/.env.test
Original file line number Diff line number Diff line change
@@ -1 +1 @@
HW_VERSION=your homework version here
HW_VERSION=C
43 changes: 38 additions & 5 deletions lesson_07/conditionals/src/lesson7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ export function compareStrings(a: string, b: string): number {
const distance = computeLexicographicDistance(a, b);

// TODO(you): Finish this method.

return 0;
if (distance < 0) {
return -1;
} else if (distance > 0) {
return 1;
} else {
return 0;
}
}

/**
Expand All @@ -24,7 +29,13 @@ export function compareStrings(a: string, b: string): number {
* @return The factorial of n.
*/
export function computeFactorial(n: number): number {
return 0;
if (n < 0) {
return 0; // Handle negative numbers
}
if (n <= 1) {
return 1;
}
return n * computeFactorial(n - 1);
}

/**
Expand All @@ -34,7 +45,21 @@ export function computeFactorial(n: number): number {
* @return An array containing the first `n` Fibonacci values.
*/
export function getFirstNFibonacciNumbers(n: number): number[] {
return [];
if (n <= 0) {
return [];
}
if (n === 1) {
return [1];
}
if (n === 2) {
return [1, 1];
}

const result = [1, 1];
for (let i = 2; i < n; i++) {
result.push(result[i - 1] + result[i - 2]);
}
return result;
}

/**
Expand Down Expand Up @@ -62,8 +87,16 @@ export function binarySearch(
// TODO(you): Finish implementing this algorithm

// If values[pivotIndex] is equal to value then return `pivotIndex`.
if (values[pivotIndex] === value) {
return pivotIndex;
}
// Else if values[pivotIndex] is greater than the value, then
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
else if (values[pivotIndex] > value) {
return binarySearch(values, start, pivotIndex - 1, value);
}
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
return -1;
else {
return binarySearch(values, pivotIndex + 1, end, value);
}
}
51 changes: 48 additions & 3 deletions lesson_07/conditionals/src/part_c.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,26 @@
* @returns
*/
export function isStrongPassword(password: string): boolean {
return false;
// Check if password has at least 8 characters
if (password.length < 8) {
return false;
}

// Check if password has at least one uppercase letter
let hasUppercase = false;
// Check if password has at least one digit
let hasDigit = false;

for (const char of password) {
if (char >= 'A' && char <= 'Z') {
hasUppercase = true;
}
if (char >= '0' && char <= '9') {
hasDigit = true;
}
}

return hasUppercase && hasDigit;
}

/**
Expand All @@ -16,7 +35,24 @@ export function isStrongPassword(password: string): boolean {
* @returns
*/
export function getDayOfWeek(day: number): string {
return "";
switch (day) {
case 0:
return "Sunday";
case 1:
return "Monday";
case 2:
return "Tuesday";
case 3:
return "Wednesday";
case 4:
return "Thursday";
case 5:
return "Friday";
case 6:
return "Saturday";
default:
return "";
}
}

/**
Expand All @@ -31,5 +67,14 @@ export function getDayOfWeek(day: number): string {
* @returns
*/
export function getTicketPrice(age: number): number {
return 0;
if (age < 5) {
return 0; // Free for children under 5
} else if (age >= 5 && age <= 17) {
return 10; // Children between 5 and 17
} else if (age >= 18 && age <= 59) {
return 20; // Adults between 18 and 59
} else if (age >= 60) {
return 15; // Seniors 60 and older
}
return 0; // Default case (shouldn't reach here)
}