diff --git a/lesson_07/conditionals/.env.test b/lesson_07/conditionals/.env.test index de8c44249..8532774b0 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=A \ No newline at end of file diff --git a/lesson_07/conditionals/package.json b/lesson_07/conditionals/package.json index f5f8dd4fd..ea54b41dd 100644 --- a/lesson_07/conditionals/package.json +++ b/lesson_07/conditionals/package.json @@ -17,18 +17,22 @@ "license": "ISC", "devDependencies": { "@alfonso-presa/soft-assert": "^0.6.0", + "@babel/core": "^7.26.10", + "@babel/preset-env": "^7.26.9", + "@babel/preset-typescript": "^7.26.0", "@eslint/js": "^9.17.0", "@types/eslint__js": "^8.42.3", "@types/jest": "^29.5.14", "@types/node": "22.10.2", + "babel-jest": "^29.7.0", "copyfiles": "^2.4.1", "eslint": "^9.17.0", "eslint-config-prettier": "^9.1.0", "jest": "^29.7.0", "prettier": "3.4.2", - "ts-jest": "^29.2.5", + "ts-jest": "^29.2.6", "ts-node": "^10.9.2", - "typescript": "^5.7.2", + "typescript": "^5.7.3", "typescript-eslint": "^8.18.0" }, "dependencies": { diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index cc44be44c..3da34bbfc 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -12,9 +12,13 @@ export function compareStrings(a: string, b: string): number { // 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 +28,17 @@ 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; + } + + let factorial = 1; + + for (let i = 1; i < n; n--) { + factorial = factorial * n; + } + + return factorial; } /** @@ -34,7 +48,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 []; + } + + const fibNums = [1, 1]; + for (let i = 2; i < n; i++) { + fibNums.push(fibNums[i - 1] + fibNums[i - 2]); + } + return fibNums; } /** @@ -61,9 +83,16 @@ export function binarySearch( // 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); + } 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; +// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value. diff --git a/lesson_07/conditionals/src/part_a.ts b/lesson_07/conditionals/src/part_a.ts index 7ad571136..be2dbb259 100644 --- a/lesson_07/conditionals/src/part_a.ts +++ b/lesson_07/conditionals/src/part_a.ts @@ -5,7 +5,11 @@ * @return True if the age corresponds to a voting age and false otherwise. */ export function canVote(age: number): boolean { - return false; + if (age >= 18) { + return true; + } else { + return false; + } } /** @@ -15,7 +19,12 @@ export function canVote(age: number): boolean { * @return The sum of all the values. */ export function addNumbers(values: number[]): number { - return 0; + let total = 0; + + for (const test of values) { + total = total + test; + } + return total; } /** @@ -25,5 +34,15 @@ export function addNumbers(values: number[]): number { * @return The factorial of n. */ export function computeFactorial(n: number): number { - return 0; + if (n < 0) { + return 0; + } + + let factorial = 1; + + for (let i = 1; i < n; n--) { + factorial = factorial * n; + } + + return factorial; }