diff --git a/lesson_07/conditionals/.env.test b/lesson_07/conditionals/.env.test index de8c44249..8532774b0 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=A \ No newline at end of file diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index cc44be44c..cca50cd0a 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -13,7 +13,8 @@ export function compareStrings(a: string, b: string): number { const distance = computeLexicographicDistance(a, b); // TODO(you): Finish this method. - + if (distance < 0) return -1; + if (distance > 0) return 1; return 0; } @@ -21,10 +22,22 @@ export function compareStrings(a: string, b: string): number { * Computes the factorial of the given value of `n`. * * @param n The value for which to compute the factorial. - * @return The factorial of n. + * @return The factorial of n, or 0 if n is negative. */ export function computeFactorial(n: number): number { - return 0; + if (n < 0) { + return 0; // Return 0 for negative numbers as per the test case + } + if (n === 0 || n === 1) { + return 1; + } + + let result = 1; + for (let i = 2; i <= n; i++) { + result *= i; + } + + return result; } /** @@ -34,9 +47,17 @@ 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 [] + } + const fibNum = [1, 1]; + for (let i = 2; i < n; i++) { + fibNum.push(fibNum[i - 1] + fibNum[i - 2]) + } + return fibNum; } + /** * Finds a value in an array of values. * @@ -58,12 +79,18 @@ export function binarySearch( } const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array. + if (values[pivotIndex] === value) { + return pivotIndex; // Value found at pivot index. + } else if (values[pivotIndex] > value) { + return binarySearch(values, start, pivotIndex - 1, value); // Search left half. + } else { + return binarySearch(values, pivotIndex + 1, end, value); // Search right half. + } +} - // TODO(you): Finish implementing this algorithm +// 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] 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. diff --git a/lesson_07/conditionals/src/part_a.ts b/lesson_07/conditionals/src/part_a.ts index 7ad571136..2318fae76 100644 --- a/lesson_07/conditionals/src/part_a.ts +++ b/lesson_07/conditionals/src/part_a.ts @@ -5,9 +5,8 @@ * @return True if the age corresponds to a voting age and false otherwise. */ export function canVote(age: number): boolean { - return false; + return age >= 18; } - /** * Adds all of the provided values and returns the sum. * @@ -15,7 +14,7 @@ export function canVote(age: number): boolean { * @return The sum of all the values. */ export function addNumbers(values: number[]): number { - return 0; + return values.reduce((sum, num) => sum + num, 0); } /** @@ -25,5 +24,8 @@ export function addNumbers(values: number[]): number { * @return The factorial of n. */ export function computeFactorial(n: number): number { - return 0; + if (n < 0) { + throw new Error("Factorial is not defined for negative numbers."); + } + return n <= 1 ? 1 : n * computeFactorial(n - 1); }