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/package-lock.json b/lesson_07/conditionals/package-lock.json index 15945122d..9f80d46cc 100644 --- a/lesson_07/conditionals/package-lock.json +++ b/lesson_07/conditionals/package-lock.json @@ -3094,21 +3094,6 @@ "dev": true, "license": "ISC" }, - "node_modules/fsevents": { - "version": "2.3.3", - "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", - "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==", - "dev": true, - "hasInstallScript": true, - "license": "MIT", - "optional": true, - "os": [ - "darwin" - ], - "engines": { - "node": "^8.16.0 || ^10.6.0 || >=11.0.0" - } - }, "node_modules/function-bind": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index cc44be44c..c0f4ca3eb 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -12,8 +12,12 @@ export function compareStrings(a: string, b: string): number { // 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; + } // TODO(you): Finish this method. - return 0; } @@ -24,9 +28,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 || n === 1) { + return 1; + } + if (n < 0) { + return 0; + } + let result = 1; + for (let i = 2; i <= n; i++) { + result *= i; + } + return result; } - /** * Returns an array of the first `n` Fibonacci numbers starting from 1. * @@ -34,7 +47,17 @@ 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]; + } + const Fibonacci: number[] = [1, 1]; + for (let i = 2; i < n; i++) { + Fibonacci.push(Fibonacci[i - 1] + Fibonacci[i - 2]); + } + return Fibonacci; } /** @@ -58,12 +81,17 @@ 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] 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; + if (values[pivotIndex] === value) { + return pivotIndex; + } else if (values[pivotIndex] > value) { + return binarySearch(values, start, pivotIndex - 1, value); // Search in the right half + } else { + return binarySearch(values, pivotIndex + 1, end, value); // Search in the left half + } } +// 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. diff --git a/lesson_07/conditionals/src/part_e.ts b/lesson_07/conditionals/src/part_e.ts index d25631c23..b02f57bf4 100644 --- a/lesson_07/conditionals/src/part_e.ts +++ b/lesson_07/conditionals/src/part_e.ts @@ -6,6 +6,39 @@ * @returns */ export function isUppercase(char: string): boolean { + const lower: string[] = [ + "A", + "B", + "C", + "D", + "E", + "F", + "G", + "H", + "I", + "J", + "K", + "L", + "M", + "N", + "O", + "P", + "Q", + "R", + "S", + "T", + "U", + "V", + "W", + "X", + "Y", + "Z", + ]; + for (const letter of lower) { + if (char === letter) { + return true; + } + } return false; } @@ -17,7 +50,11 @@ export function isUppercase(char: string): boolean { * @returns */ export function canGetDriverLicense(age: number, passedTest: boolean): boolean { - return false; + if (age >= 18 && passedTest === true) { + return true; + } else { + return false; + } } /** @@ -29,5 +66,9 @@ export function canGetDriverLicense(age: number, passedTest: boolean): boolean { * @returns */ export function isStoreOpen(day: string, hour: number): boolean { - return false; + if (day !== "Sunday" && hour < 21 && hour >= 9) { + return true; + } else { + return false; + } }