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..3da34bbfc 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,17 @@ 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; + } + + let factorial = 1; + + for (let i = 1; i < n; n--) { + factorial = factorial * n; + } + + return factorial; } /** @@ -34,7 +48,15 @@ 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 fibNums = [1, 1]; + for (let i = 2; i < n; i++) { + fibNums.push(fibNums[i - 1] + fibNums[i - 2]); + } + return fibNums; } /** @@ -61,9 +83,16 @@ 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) { + 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. diff --git a/lesson_07/conditionals/src/part_a.ts b/lesson_07/conditionals/src/part_a.ts index 7ad571136..be2dbb259 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,12 @@ export function canVote(age: number): boolean { * @return The sum of all the values. */ export function addNumbers(values: number[]): number { - return 0; + let total = 0; + + for (const test of values) { + total = total + test; + } + return total; } /** @@ -25,5 +34,15 @@ export function addNumbers(values: number[]): number { * @return The factorial of n. */ export function computeFactorial(n: number): number { - return 0; + if (n < 0) { + return 0; + } + + let factorial = 1; + + for (let i = 1; i < n; n--) { + factorial = factorial * n; + } + + return factorial; }