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..b3eaba90d 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -11,6 +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); + { + if (distance < 0) { + return -1; + } else if (distance > 0) { + return 1; + } else { + return 0; + } + } // TODO(you): Finish this method. @@ -24,7 +33,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 1; + } else if (n < 0) { + return 0; + } else if (n === 1) { + return 1; + } else { + return n * computeFactorial(n - 1); + } } /** @@ -34,7 +51,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 []; + if (n === 1) return [1]; + if (n === 2) return [1, 1]; + + const fibonacciNumbers = [1, 1]; + for (let i = 2; i < n; i++) { + fibonacciNumbers.push(fibonacciNumbers[i - 1] + fibonacciNumbers[i - 2]); + } + return fibonacciNumbers; } /** @@ -60,7 +85,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, 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; diff --git a/lesson_07/conditionals/src/part_d.ts b/lesson_07/conditionals/src/part_d.ts index c30cbac2f..a09258ebe 100644 --- a/lesson_07/conditionals/src/part_d.ts +++ b/lesson_07/conditionals/src/part_d.ts @@ -7,6 +7,9 @@ * @returns */ export function isWithinRange(num: number, min: number, max: number): boolean { + if (num >= min && num <= max) { + return true; + } return false; } @@ -19,7 +22,12 @@ export function isWithinRange(num: number, min: number, max: number): boolean { * @returns */ export function isValidTriangle(a: number, b: number, c: number): boolean { - return false; + switch (true) { + case a + b > c && a + c > b && b + c > a: + return true; + default: + return false; + } } /** @@ -30,5 +38,17 @@ export function isValidTriangle(a: number, b: number, c: number): boolean { * @returns */ export function getSeason(month: number): string { + if (isWithinRange(month, 1, 12)) { + switch (true) { + case isWithinRange(month, 3, 5): + return "Spring"; + case isWithinRange(month, 6, 8): + return "Summer"; + case isWithinRange(month, 9, 11): + return "Fall"; + default: + return "Winter"; + } + } return "Invalid month"; }