diff --git a/lesson_07/conditionals/.env.test b/lesson_07/conditionals/.env.test index de8c44249..6823e4a7d 100644 --- a/lesson_07/conditionals/.env.test +++ b/lesson_07/conditionals/.env.test @@ -1 +1 @@ -HW_VERSION=your homework version here \ No newline at end of file +HW_VERSION=C \ No newline at end of file diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index cc44be44c..600631084 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -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; + } } /** @@ -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); } /** @@ -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; } /** @@ -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); + } } diff --git a/lesson_07/conditionals/src/part_c.ts b/lesson_07/conditionals/src/part_c.ts index 6effb3b47..479e4a3f1 100644 --- a/lesson_07/conditionals/src/part_c.ts +++ b/lesson_07/conditionals/src/part_c.ts @@ -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; } /** @@ -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 ""; + } } /** @@ -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) }