diff --git a/lesson_07/conditionals/.env.test b/lesson_07/conditionals/.env.test index de8c44249..6823e4a7d 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=C \ No newline at end of file diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index cc44be44c..d53505af7 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -11,10 +11,13 @@ 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 +27,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 0; + } else { + let result = 1; + for (let i = 2; i <= n; i++) { + result = result * i; + } + return result; + } } /** @@ -34,7 +45,16 @@ 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 []; + } + const resultsArr: number[] = [1, 1]; + if (n > 1) { + for (let i = 2; i < n; i++) { + resultsArr.push(resultsArr[i - 1] + resultsArr[i - 2]); + } + } + return resultsArr; } /** @@ -60,7 +80,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, pivotIndex + 1, end, value); + } else { + return binarySearch(values, start, pivotIndex - 1, 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_c.ts b/lesson_07/conditionals/src/part_c.ts index 6effb3b47..683f85d24 100644 --- a/lesson_07/conditionals/src/part_c.ts +++ b/lesson_07/conditionals/src/part_c.ts @@ -6,6 +6,9 @@ * @returns */ export function isStrongPassword(password: string): boolean { + if (password.length >= 8) { + return /\d/.test(password) && /[A-Z]/.test(password); + } return false; } @@ -16,7 +19,33 @@ export function isStrongPassword(password: string): boolean { * @returns */ export function getDayOfWeek(day: number): string { - return ""; + let result = ""; + switch (day) { + case 0: + result = "Sunday"; + break; + case 1: + result = "Monday"; + break; + case 2: + result = "Tuesday"; + break; + case 3: + result = "Wednesday"; + break; + case 4: + result = "Thursday"; + break; + case 5: + result = "Friday"; + break; + case 6: + result = "Saturday"; + break; + default: + result = ""; + } + return result; } /** @@ -31,5 +60,13 @@ export function getDayOfWeek(day: number): string { * @returns */ export function getTicketPrice(age: number): number { - return 0; + if (age < 5) { + return 0; + } else if (age >= 5 && age <= 17) { + return 10; + } else if (age >= 18 && age <= 59) { + return 20; + } else { + return 15; + } }