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..0285e7eef 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -13,8 +13,12 @@ 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; + } + return distance; } /** @@ -24,7 +28,19 @@ export function compareStrings(a: string, b: string): number { * @return The factorial of n. */ export function computeFactorial(n: number): number { - return 0; + for (let i = n - 1; i > 0; i--) { + n *= i; + } + if (n == 0) { + return 1; + } + if (n == 1) { + return 1; + } + if (n < 0) { + return 0; + } + return n; } /** @@ -34,6 +50,22 @@ export function computeFactorial(n: number): number { * @return An array containing the first `n` Fibonacci values. */ export function getFirstNFibonacciNumbers(n: number): number[] { + if (n <= 0) { + return []; + } + if (n === 1) { + return [1]; + } + if (n === 2) { + return [1, 1]; + } + if (n > 2) { + const fib = [1, 1]; + for (let i = 2; i < n; i++) { + fib.push(fib[i - 1] + fib[i - 2]); + } + return fib; + } return []; } @@ -65,5 +97,12 @@ export function binarySearch( // 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. + 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); + } return -1; } diff --git a/lesson_07/conditionals/src/part_a.ts b/lesson_07/conditionals/src/part_a.ts index 7ad571136..a73ebbf29 100644 --- a/lesson_07/conditionals/src/part_a.ts +++ b/lesson_07/conditionals/src/part_a.ts @@ -5,7 +5,11 @@ * @return True if the age corresponds to a voting age and false otherwise. */ export function canVote(age: number): boolean { - return false; + if (age >= 18) { + return true; + } else { + return false; + } } /** @@ -15,7 +19,11 @@ export function canVote(age: number): boolean { * @return The sum of all the values. */ export function addNumbers(values: number[]): number { - return 0; + let sum = 0; + for (const val of values) { + sum += val; + } + return sum; } /** @@ -25,5 +33,17 @@ export function addNumbers(values: number[]): number { * @return The factorial of n. */ export function computeFactorial(n: number): number { - return 0; + for (let i = n - 1; i > 0; i--) { + n *= i; + } + if (n == 0) { + return 1; + } + if (n == 1) { + return 1; + } + if (n < 0) { + return 0; + } + return n; }