diff --git a/lesson_07/conditionals/.env.test b/lesson_07/conditionals/.env.test index de8c44249..6da045feb 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=B diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index cc44be44c..65e9b674d 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -11,11 +11,12 @@ export function compareStrings(a: string, b: string): number { // The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1 // if it is greater, and 0 if the strings are equal. const distance = computeLexicographicDistance(a, b); + return distance; +} - // TODO(you): Finish this method. +// TODO(you): Finish this method. - return 0; -} +// Removed misplaced return statement /** * Computes the factorial of the given value of `n`. @@ -24,7 +25,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; + } + if (n === 1) { + return 1; + } + if (n === 2) { + return 2; + } + let result = 1; + for (let i = 2; i <= n; ++i) { + result *= i; + } + return result; } /** @@ -34,7 +48,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 < 1) { + return []; + } + if (n === 1) { + return [1]; + } + if (n === 2) { + return [1, 1]; + } + const sequence = [1, 1]; + for (let i = 2; i < n; ++i) { + sequence[i] = sequence[i - 1] + sequence[i - 2]; + } + return sequence; } /** @@ -56,14 +83,19 @@ export function binarySearch( // The range is not valid so just return -1. return -1; } - - const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array. - // 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; + + const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array. + 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); + } } diff --git a/lesson_07/conditionals/src/part_b.ts b/lesson_07/conditionals/src/part_b.ts index 22fc01ccc..adc19b6d6 100644 --- a/lesson_07/conditionals/src/part_b.ts +++ b/lesson_07/conditionals/src/part_b.ts @@ -5,7 +5,15 @@ * @returns */ export function isLeapYear(year: number): boolean { - return false; + if (year % 400 === 0) { + return true; + } else if (year % 100 === 0) { + return false; + } else if (year % 4 === 0) { + return true; + } else { + return false; + } } /** @@ -15,7 +23,11 @@ export function isLeapYear(year: number): boolean { * @returns */ export function isEvenOrOdd(num: number): string { - return ""; + if (num % 2 === 0) { + return "even"; + } else { + return "odd"; + } } /** @@ -25,5 +37,22 @@ export function isEvenOrOdd(num: number): string { * @returns */ export function hasVowel(word: string): boolean { - return false; + const lowerCaseword = word.toLowerCase(); + if (lowerCaseword.includes("a")) { + return true; + } + if (lowerCaseword.includes("e")) { + return true; + } + if (lowerCaseword.includes("i")) { + return true; + } + if (lowerCaseword.includes("o")) { + return true; + } + if (lowerCaseword.includes("u")) { + return true; + } else { + return false; + } }