diff --git a/lesson_07/conditionals/package-lock.json b/lesson_07/conditionals/package-lock.json index 6c2e02652..b91951020 100644 --- a/lesson_07/conditionals/package-lock.json +++ b/lesson_07/conditionals/package-lock.json @@ -21,7 +21,7 @@ "prettier": "3.3.3", "ts-jest": "^29.2.5", "ts-node": "^10.9.2", - "typescript": "^5.6.2", + "typescript": "^5.6.3", "typescript-eslint": "^8.7.0" } }, @@ -5335,9 +5335,9 @@ } }, "node_modules/typescript": { - "version": "5.6.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.6.2.tgz", - "integrity": "sha512-NW8ByodCSNCwZeghjN3o+JX5OFH0Ojg6sadjEKY4huZ52TqbJTJnDo5+Tw98lSy63NZvi4n+ez5m2u5d4PkZyw==", + "version": "5.6.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.6.3.tgz", + "integrity": "sha512-hjcS1mhfuyi4WW8IWtjP7brDrG2cuDZukyrYrSauoXGNgx0S7zceP07adYkJycEr56BOUTNPzbInooiN3fn1qw==", "dev": true, "license": "Apache-2.0", "bin": { diff --git a/lesson_07/conditionals/package.json b/lesson_07/conditionals/package.json index 6e09f98e0..1298e73da 100644 --- a/lesson_07/conditionals/package.json +++ b/lesson_07/conditionals/package.json @@ -28,7 +28,7 @@ "prettier": "3.3.3", "ts-jest": "^29.2.5", "ts-node": "^10.9.2", - "typescript": "^5.6.2", + "typescript": "^5.6.3", "typescript-eslint": "^8.7.0" } -} \ No newline at end of file +} diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index 52f45df8b..32bb67393 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -6,10 +6,14 @@ import { computeLexicographicDistance } from "./util.js"; * @param age The age to check. * @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; + } } - /** * Compares two strings lexicographically. * @@ -22,9 +26,15 @@ export function compareStrings(a: string, b: string): number { // if it is greater, and 0 if the strings are equal. const distance = computeLexicographicDistance(a, b); + //This is just an idea(That's probably wrong)/ // TODO(you): Finish this method. - - return 0; + let stringLengthCompare = 0; + if (distance > 0) { + stringLengthCompare = 1; //equal distances + } else if (distance < 0) { + stringLengthCompare = -1; //unequal distances + } + return stringLengthCompare; } /** @@ -37,7 +47,27 @@ export function compareStrings(a: string, b: string): number { * @return The letter grade ("A+", "A", "A-", "B+", etc.). */ export function convertGpaToLetterGrade(gpa: number): string { - return "F"; + if (gpa >= 4.0) { + return "A"; + } else if (gpa <= 3.99 && gpa >= 3.7) { + return "A-"; + } else if (gpa <= 3.69 && gpa >= 3.3) { + return "B+"; + } else if (gpa <= 3.29 && gpa >= 3.0) { + return "B"; + } else if (gpa <= 2.99 && gpa >= 2.7) { + return "B-"; + } else if (gpa <= 2.69 && gpa >= 2.3) { + return "C+"; + } else if (gpa <= 2.29 && gpa >= 2.0) { + return "C"; + } else if (gpa <= 1.99 && gpa >= 1.7) { + return "C-"; + } else if (gpa <= 1.69 && gpa >= 1.3) { + return "D+"; + } else if (gpa <= 1.29 && gpa >= 1.0) { + return "D"; + } else return "F"; } /** @@ -47,9 +77,16 @@ export function convertGpaToLetterGrade(gpa: number): string { * @return The factorial of n. */ export function computeFactorial(n: number): number { - return 0; + let product = 1; + for (let i = 1; i <= n; i++) { + product *= i; + } + + return product; } +const n = 2; +console.log(computeFactorial(n)); /** * Adds all of the provided values and returns the sum. * @@ -57,7 +94,11 @@ export function computeFactorial(n: number): number { * @return The sum of all the values. */ export function addNumbers(values: number[]): number { - return 0; + let sum = 0; + for (const value of values) { + sum += value; + } + return sum; } /** @@ -67,14 +108,24 @@ export function addNumbers(values: number[]): number { * @return An array containing the first `n` Fibonacci values. */ export function getFirstNFibonacciNumbers(n: number): number[] { - return []; + if (n < 1) { + //return []; + } + + const fibonacci: number[] = [1, 1]; // The function starts with the first two Fibonacci numbers + + for (let i = 2; i < n; i++) { + fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2]; // Here the sum of the preceeding expression(?) is iterated with the number directly preceeding it + } + + return fibonacci.slice(0, n); // Return only the first n Fibonacci numbers } /** * Finds a value in an array of values. * * @param values The values to search. - * @param start The left most index to search. + * @param start The left most index to search.e * @param end The right most index to search. * @param value The value to look for. * @return The index of the value if found in the array and -1 otherwise. @@ -86,12 +137,19 @@ export function binarySearch( value: number, ): number { if (end < start) { - // The range is not valid so just return -1. return -1; } 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); + return 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`.