diff --git a/lesson_07/conditionals/.env.test b/lesson_07/conditionals/.env.test index de8c44249..f811b9059 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=D \ No newline at end of file diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index cc44be44c..046d44bd0 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -11,10 +11,15 @@ 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 +29,18 @@ export function compareStrings(a: string, b: string): number { * @return The factorial of n. */ export function computeFactorial(n: number): number { - return 0; + // edge cases + if (n < 0) { + return 0; + } else if (n < 2) { + return 1; + } + + let result = 1; + for (let i = n; i > 0; i--) { + result *= i; + } + return result; } /** @@ -34,7 +50,23 @@ export function computeFactorial(n: number): number { * @return An array containing the first `n` Fibonacci values. */ export function getFirstNFibonacciNumbers(n: number): number[] { - return []; + const arr: number[] = []; + let curr = 1; + let prev = 1; + let next; + + for (let i = 0; i < n; i++) { + if (i < 2) { + arr[i] = 1; + } else { + next = curr + prev; + arr[i] = next; + prev = curr; + curr = next; + } + } + + return arr; } /** @@ -65,5 +97,13 @@ 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 (value === values[pivotIndex]) { + return pivotIndex; + } else if (value < values[pivotIndex]) { + 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_d.ts b/lesson_07/conditionals/src/part_d.ts index c30cbac2f..acee75ae7 100644 --- a/lesson_07/conditionals/src/part_d.ts +++ b/lesson_07/conditionals/src/part_d.ts @@ -7,7 +7,11 @@ * @returns */ export function isWithinRange(num: number, min: number, max: number): boolean { - return false; + if (num >= min && num <= max) { + return true; + } else { + return false; + } } /** @@ -19,7 +23,11 @@ export function isWithinRange(num: number, min: number, max: number): boolean { * @returns */ export function isValidTriangle(a: number, b: number, c: number): boolean { - return false; + if (a + b > c && a + c > b && b + c > a) { + return true; + } else { + return false; + } } /** @@ -30,5 +38,34 @@ export function isValidTriangle(a: number, b: number, c: number): boolean { * @returns */ export function getSeason(month: number): string { + const winter: number[] = [12, 1, 2]; + const spring: number[] = [3, 4, 5]; + const summer: number[] = [6, 7, 8]; + const fall: number[] = [9, 10, 11]; + + for (const num of winter) { + if (num === month) { + return "Winter"; + } + } + + for (const num of spring) { + if (num === month) { + return "Spring"; + } + } + + for (const num of summer) { + if (num === month) { + return "Summer"; + } + } + + for (const num of fall) { + if (num === month) { + return "Fall"; + } + } + return "Invalid month"; }