diff --git a/lesson_07/conditionals/.env.test b/lesson_07/conditionals/.env.test index de8c44249..6f05a4e86 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=F diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index cc44be44c..2084a7d63 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -13,8 +13,16 @@ export function compareStrings(a: string, b: string): number { const distance = computeLexicographicDistance(a, b); // TODO(you): Finish this method. - - return 0; + if (distance < 0) { + // a is less than b + return -1; + } else if (distance > 0) { + // a is greater than b + return 1; + } else { + // a is equal to b + return 0; + } } /** @@ -24,7 +32,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; // Factorial of negative numbers is undefined, return 0 + } + + if (n === 0 || n === 1) { + return 1; // Factorial of 0 and 1 is 1 + } + + let result = 1; + for (let i = 2; i <= n; i++) { + result *= i; + } + return result; } /** @@ -34,7 +54,18 @@ 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]; // First Fibonacci number is 1 + } + const fibonacci = [1, 1]; // Start with first two Fibonacci numbers + for (let i = 2; i < n; i++) { + // Each new number is the sum of the previous two + fibonacci.push(fibonacci[i - 1] + fibonacci[i - 2]); + } + return fibonacci; } /** @@ -61,9 +92,13 @@ export function binarySearch( // TODO(you): Finish implementing this algorithm - // If values[pivotIndex] is equal to value then return `pivotIndex`. - // Else if values[pivotIndex] is greater than the value, then - // call `binarySearch(values, start, pivotIndex - 1, value)` and return its value; - // Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value. - return -1; + if (values[pivotIndex] === value) { + // If values[pivotIndex] is equal to value then return `pivotIndex`. + return pivotIndex; + } else if (values[pivotIndex] > value) { + // Else if values[pivotIndex] is greater than the value, then + return binarySearch(values, start, pivotIndex - 1, value); // call `binarySearch(values, start, pivotIndex - 1, value)` and return its value; + } else { + return binarySearch(values, pivotIndex + 1, end, value); // Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value. + } } diff --git a/lesson_07/conditionals/src/part_f.ts b/lesson_07/conditionals/src/part_f.ts index fdd8cf052..3ad097f08 100644 --- a/lesson_07/conditionals/src/part_f.ts +++ b/lesson_07/conditionals/src/part_f.ts @@ -5,8 +5,19 @@ * @param score * @returns */ + export function getLetterGrade(score: number): string { - return ""; + if (score >= 90 && score <= 100) { + return "A"; + } else if (score >= 80 && score <= 89) { + return "B"; + } else if (score >= 70 && score <= 79) { + return "C"; + } else if (score >= 60 && score <= 69) { + return "D"; + } else { + return "F"; + } } /** @@ -16,7 +27,14 @@ export function getLetterGrade(score: number): string { * @returns */ export function sumEvenNumbers(numbers: number[]): number { - return 0; + let sum = 0; + + for (const number of numbers) { + if (number % 2 === 0) { + sum += number; + } + } + return sum; } /** @@ -27,5 +45,14 @@ export function sumEvenNumbers(numbers: number[]): number { * @returns */ export function countCharacter(text: string, character: string): number { - return 0; + let count = 0; + + for (const char of text) { + if (char === character) { + count++; // Increment count if character matches + } + } + return count; } + +