diff --git a/lesson_07/conditionals/.env.test b/lesson_07/conditionals/.env.test index de8c44249..1eeddbff6 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 \ No newline at end of file diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index cc44be44c..39018dc4d 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -11,10 +11,13 @@ 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); - - // TODO(you): Finish this method. - - return 0; + if (distance < 0) { + return -1; + } else if (distance > 0) { + return 1; + } else { + return 0; + } } /** @@ -24,7 +27,20 @@ export function compareStrings(a: string, b: string): number { * @return The factorial of n. */ export function computeFactorial(n: number): number { - return 0; + let product = 1; + + if (n < 0) { + return 0; + } + + if (n <= 1) { + return 1; + } + + for (let i = 1; i <= n; i++) { + product = i * product; + } + return product; } /** @@ -34,7 +50,25 @@ export function computeFactorial(n: number): number { * @return An array containing the first `n` Fibonacci values. */ export function getFirstNFibonacciNumbers(n: number): number[] { - return []; + let fib: number[] = []; + + if (n <= 0) { + return []; + } + + if (n === 1) { + return [1]; + } + + if (n === 2) { + return [1, 1]; + } + fib = [1, 1]; + for (let i = 2; i < n; i++) { + const fibValue = fib[i - 1] + fib[i - 2]; + fib.push(fibValue); + } + return fib; } /** @@ -60,10 +94,14 @@ 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] 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 + // 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 binarySearch(values, pivotIndex +1, end, value); +} \ No newline at end of file diff --git a/lesson_07/conditionals/src/part_b.ts b/lesson_07/conditionals/src/part_b.ts index 22fc01ccc..fc35c8b6e 100644 --- a/lesson_07/conditionals/src/part_b.ts +++ b/lesson_07/conditionals/src/part_b.ts @@ -5,6 +5,15 @@ * @returns */ export function isLeapYear(year: number): boolean { + if (year % 400 === 0) { + return true; // Divisible by 400 + } + if (year % 100 === 0) { + return false; // Divisible by 100 but not 400 + } + if (year % 4 === 0) { + return true; // Divisible by 4 but not 100 + } return false; } @@ -15,7 +24,9 @@ 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 +36,12 @@ export function isEvenOrOdd(num: number): string { * @returns */ export function hasVowel(word: string): boolean { + const v = ["a", "e", "i", "o", "u"]; + for (let i = 0; i < word.length; i++) { + if (v.includes(word.charAt(i))) { + return true; + } + } + return false; }