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..43619768f 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -11,10 +11,7 @@ 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; + return distance; } /** @@ -24,7 +21,15 @@ 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; + } else { + let result = 1; + for (let i = 2; i <= n; i++) { + result = result * i; + } + return result; + } } /** @@ -34,7 +39,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; } /** @@ -59,7 +77,13 @@ 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] === value) { + return pivotIndex; + } else if (values[pivotIndex] < value) { + return binarySearch(values, pivotIndex + 1, end, value); + } else { + return binarySearch(values, start, pivotIndex - 1, value); + } // If values[pivotIndex] is equal to value then return `pivotIndex`. // Else if values[pivotIndex] is greater than the value, then diff --git a/lesson_07/conditionals/src/part_b.ts b/lesson_07/conditionals/src/part_b.ts index 22fc01ccc..22a143e4d 100644 --- a/lesson_07/conditionals/src/part_b.ts +++ b/lesson_07/conditionals/src/part_b.ts @@ -5,17 +5,30 @@ * @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; + } } /** * Returns whether the given number is even or odd. - * * @param num * @returns */ +// To determine if anumber is even is has to be divisible by 2 with no remainder. +// To determine if a number is odd, it will not be divisible by 2 evenly. export function isEvenOrOdd(num: number): string { - return ""; + if (num % 2 === 0) { + return "even"; + } else { + return "odd"; + } } /** @@ -24,6 +37,26 @@ export function isEvenOrOdd(num: number): string { * @param word * @returns */ +// If any word contains a vowel, it will render true. +// If a word does not contain a vowel, it will render false. + 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; + } }