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..e83df017e 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -12,9 +12,13 @@ export function compareStrings(a: string, b: string): number { // if it is greater, and 0 if the strings are equal. 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 +28,20 @@ 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 is not defined for negative numbers. + } + if (n === 0 || n === 1) { + return 1; // The factorial of 0 and 1 is 1. + } + let result = 1; + for (let i = 2; i <= n; i++) { + result *= i; // This is a shortcut for result = result * i + } + return result; // Return the computed factorial. + // If n is less than 0, return 0. + // If n is 0 or 1, return 1. + // Otherwise, compute the factorial iteratively from 2 to n. } /** @@ -34,7 +51,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 []; + } + if (n === 1) { + return [1]; + } + const fibonacciNumbers = [1, 1]; // Start with the first two numbers + while (fibonacciNumbers.length < n) { + const lastIndex = fibonacciNumbers.length - 1; + const nextFibonacciNumber = + fibonacciNumbers[lastIndex] + fibonacciNumbers[lastIndex - 1]; + fibonacciNumbers.push(nextFibonacciNumber); + } + return fibonacciNumbers; } /** @@ -59,11 +89,18 @@ export function binarySearch( const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array. - // TODO(you): Finish implementing this algorithm + 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); + } + // 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 the value is not found, return -1. } diff --git a/lesson_07/conditionals/src/part_f.ts b/lesson_07/conditionals/src/part_f.ts index fdd8cf052..b0fca4fc3 100644 --- a/lesson_07/conditionals/src/part_f.ts +++ b/lesson_07/conditionals/src/part_f.ts @@ -6,9 +6,21 @@ * @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 if (score < 60) { + return "F"; + } + return "Invalid score"; // In case of an invalid score } + /** * Calculates the sum of all even numbers in the given array. * @@ -16,9 +28,16 @@ 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; } + /** * Counts how many times a specific character appears in a string. * @@ -27,5 +46,11 @@ export function sumEvenNumbers(numbers: number[]): number { * @returns */ export function countCharacter(text: string, character: string): number { - return 0; + let count = 0; + for (let i = 0; i < text.length; i++) { + if (text[i] === character) { + count++; + } + } + return count; }