diff --git a/lesson_07/conditionals/.env.test b/lesson_07/conditionals/.env.test index de8c44249..9fd9f468b 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=E \ No newline at end of file diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index cc44be44c..915f35e44 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -10,13 +10,16 @@ import { computeLexicographicDistance } from "./util.js"; 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); + 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 +27,16 @@ 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; // Base case: 0! and 1! both equal 1 + } + else if (n === 1) { + return 1; + } + else if (n < 0) { + return 0; + } + return n * computeFactorial(n - 1); } /** @@ -34,6 +46,22 @@ export function computeFactorial(n: number): number { * @return An array containing the first `n` Fibonacci values. */ export function getFirstNFibonacciNumbers(n: number): number[] { + for (let i = 0; i < n; i++) { + if (n === 0) { + return []; + } + else if (n === 1) { + return [1]; + } + 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 []; } @@ -57,9 +85,19 @@ export function binarySearch( return -1; } - const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array. - // TODO(you): Finish implementing this algorithm + const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array. + 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); + return -1; + } + + + // 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_e.ts b/lesson_07/conditionals/src/part_e.ts index d25631c23..76e7f7a41 100644 --- a/lesson_07/conditionals/src/part_e.ts +++ b/lesson_07/conditionals/src/part_e.ts @@ -6,6 +6,10 @@ * @returns */ export function isUppercase(char: string): boolean { + if (char >= "A" && char <= "Z") { + return true; + } + return false; } @@ -17,6 +21,9 @@ export function isUppercase(char: string): boolean { * @returns */ export function canGetDriverLicense(age: number, passedTest: boolean): boolean { + if (age >= 18 && passedTest) { + return true; + } return false; } @@ -29,5 +36,9 @@ export function canGetDriverLicense(age: number, passedTest: boolean): boolean { * @returns */ export function isStoreOpen(day: string, hour: number): boolean { - return false; + if (day === "Sunday" || hour < 9 || hour >= 21) { + return false; + } else { + return true; + } }