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/package-lock.json b/lesson_07/conditionals/package-lock.json index 15945122d..00c9d7945 100644 --- a/lesson_07/conditionals/package-lock.json +++ b/lesson_07/conditionals/package-lock.json @@ -5769,4 +5769,4 @@ } } } -} +} \ No newline at end of file diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index cc44be44c..c5aae8e93 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -7,15 +7,23 @@ import { computeLexicographicDistance } from "./util.js"; * @param b The second `string` to compare. * @return -1 if a is less than b, 1 if a is greater than b, and 0 otherwise. */ -export function compareStrings(a: string, b: string): number { +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; + } + +} /** * Computes the factorial of the given value of `n`. @@ -24,7 +32,18 @@ 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 === 1) { + return 1; + } + else if (n < 0) { + return 0; + } + else { + return n * computeFactorial(n-1); + } } /** @@ -34,7 +53,21 @@ export function computeFactorial(n: number): number { * @return An array containing the first `n` Fibonacci values. */ export function getFirstNFibonacciNumbers(n: number): number[] { - return []; + + for (let i = 0; i < n; i++) { + if (n === 0) + return []; + + else if (n === 2) { + return[1,1]; + } + else { + const fib = getFirstNFibonacciNumbers(n -1); + fib.push(fib[fib.length- 1] + fib[fib.length - 2]); + return fib; + } + } + return[]; } /** @@ -52,18 +85,24 @@ export function binarySearch( end: number, value: number, ): number { - if (end < start) { - // The range is not valid so just return -1. - return -1; + if (start > end) { + return -1; // base case: not found } - const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array. + const pivotIndex = Math.floor((start + end) / 2); + + 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); + } +} // 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; -} + // Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value. \ No newline at end of file diff --git a/lesson_07/conditionals/src/part_d.ts b/lesson_07/conditionals/src/part_d.ts index c30cbac2f..a3554d9c1 100644 --- a/lesson_07/conditionals/src/part_d.ts +++ b/lesson_07/conditionals/src/part_d.ts @@ -7,8 +7,11 @@ * @returns */ export function isWithinRange(num: number, min: number, max: number): boolean { - return false; -} + return num >= min && num <= max; +} + +console.log(isWithinRange(7, 5, 10)); +console.log(isWithinRange(2, 5, 10)); /** * Determine if a shape is a triangle based on the given side lengths. @@ -19,7 +22,7 @@ export function isWithinRange(num: number, min: number, max: number): boolean { * @returns */ export function isValidTriangle(a: number, b: number, c: number): boolean { - return false; + return a + b > c && a + c > b && b + c > a; } /** @@ -30,5 +33,15 @@ export function isValidTriangle(a: number, b: number, c: number): boolean { * @returns */ export function getSeason(month: number): string { - return "Invalid month"; -} + if (month >= 3 && month <= 5) { + return "Spring"; + } else if (month >= 6 && month <= 8) { + return "Summer"; + } else if (month >= 9 && month <= 11) { + return "Fall"; + } else if (month === 12 || month === 1 || month === 2) { + return "Winter"; + } else { + return "Invalid month"; + } +} \ No newline at end of file