diff --git a/lesson_07/conditionals/.env.test b/lesson_07/conditionals/.env.test index de8c44249..b4dbb49ab 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= G \ No newline at end of file diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index cc44be44c..5767af750 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -11,9 +11,11 @@ export function compareStrings(a: string, b: string): number { // The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1 // if it is greater, and 0 if the strings are equal. const distance = computeLexicographicDistance(a, b); - - // TODO(you): Finish this method. - + if (distance < 0) { + return -1; + } else if (distance > 0) { + return 1; + } return 0; } @@ -24,7 +26,19 @@ 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; + } + + if (n === 0) { + return 1; + } + + let result = 1; + for (let i = 1; i <= n; i++) { + result *= i; + } + return result; } /** @@ -34,7 +48,20 @@ 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 []; // Return an empty array for non-positive lengths + } else if (n === 1) { + return [1]; // The first Fibonacci number is 1 (as per the request) + } + + const fibArray: number[] = [1, 1]; // Initialize with the first two Fibonacci numbers starting from 1 + + for (let i = 2; i < n; i++) { + const nextFib = fibArray[i - 1] + fibArray[i - 2]; + fibArray.push(nextFib); + } + + return fibArray; } /** @@ -46,6 +73,7 @@ export function getFirstNFibonacciNumbers(n: number): number[] { * @param value The value to look for. * @return The index of the value if found in the array and -1 otherwise. */ + export function binarySearch( values: number[], start: number, @@ -57,8 +85,14 @@ export function binarySearch( return -1; } - const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array. - + const pivotIndex = Math.floor((start + end) / 2); + if (values[pivotIndex] == value) { + return pivotIndex; + } else if (values[pivotIndex] > value) { + return binarySearch(values, start, pivotIndex - 1, value); + } else { + return binarySearch(values, pivotIndex + 1, end, value); + } // TODO(you): Finish implementing this algorithm // If values[pivotIndex] is equal to value then return `pivotIndex`. diff --git a/lesson_07/conditionals/src/part_g.ts b/lesson_07/conditionals/src/part_g.ts index 766376b61..ae970199e 100644 --- a/lesson_07/conditionals/src/part_g.ts +++ b/lesson_07/conditionals/src/part_g.ts @@ -5,7 +5,17 @@ * @returns */ export function findLargestNumber(numbers: number[]): number { - return 0; + if (numbers.length === 0) { + return 0; + } + + let largest = numbers[0]; + for (let i = 1; i < numbers.length; i++) { + if (numbers[i] > largest) { + largest = numbers[i]; + } + } + return largest; } /** @@ -16,6 +26,10 @@ export function findLargestNumber(numbers: number[]): number { * @returns */ export function isPalindrome(text: string): boolean { + const cleaned = text.replace(/[^a-zA-Z0-9]/g, "").toLowerCase(); + + return cleaned === cleaned.split("").reverse().join(""); + return false; } @@ -34,7 +48,28 @@ export function daysUntilBirthday( currentMonth: number, currentDay: number, birthMonth: number, - birthDay: number + birthDay: number, ): number { - return 0; + const today = new Date(); + const currentYear = today.getFullYear(); + + // Create a date for the birthday this year + let nextBirthday = new Date(currentYear, birthMonth - 1, birthDay); + + // If the birthday has already passed this year, set it for next year + if ( + birthMonth < currentMonth || + (birthMonth === currentMonth && birthDay < currentDay) + ) { + nextBirthday = new Date(currentYear + 1, birthMonth - 1, birthDay); + } + + const currentDate = new Date(currentYear, currentMonth - 1, currentDay); + + // Calculate the difference in milliseconds and convert to days + const diffTime = nextBirthday.getTime() - currentDate.getTime(); + const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); + + return diffDays; } +