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..b8b436800 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -1,16 +1,27 @@ -import { computeLexicographicDistance } from "./util.js"; +//import { computeLexicographicDistance } from "./util.js"; -/** +/** (Q1) * Returns true if the provided age meets the minimum US voting age and false otherwise. * * @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; + return age >= 18; } -/** +const age = 12; +let checkAge; + +if (canVote(age)) { + checkAge = true; +} else { + checkAge = false; +} + +console.log(checkAge); + +/** (Q2) * Compares two strings lexicographically. * * @param a The first `string` to compare. @@ -18,16 +29,27 @@ export function canVote(age: number): boolean { * @return -1 if a is less than b, 1 if a is greater than b, and 0 otherwise. */ 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); + return distance; +} - // TODO(you): Finish this method. - - return 0; +export function computeLexicographicDistance(a: string, b: string): number { + + if (a < b) { + return -1; + } else if (a > b) { + return 1; + } else { + return 0; + } } -/** +const result = compareStrings("Kimberlee", "haldane"); +console.log(result); + + + +/** (Q3) * Converts a GPA on the 4.0 scale to the corresponding letter grade using the college board * scale. See * https://bigfuture.collegeboard.org/plan-for-college/college-basics/how-to-convert-gpa-4.0-scale @@ -36,41 +58,104 @@ export function compareStrings(a: string, b: string): number { * @param gpa The GPA value. * @return The letter grade ("A+", "A", "A-", "B+", etc.). */ +export function myGrade(grade: number): string { + const gpa = convertGpaToLetterGrade(grade); + return gpa; +} + + export function convertGpaToLetterGrade(gpa: number): string { - return "F"; + if (gpa >= 4.0) { + return "A"; + } else if (gpa >= 3.7) { + return "A-"; + } else if (gpa >= 3.3) { + return "B+"; + } else if (gpa >= 3.0) { + return "B"; + } else if (gpa >= 2.7) { + return "B-"; + } else if (gpa >= 2.3) { + return "C+"; + } else if (gpa >= 2.0) { + return "C"; + } else if (gpa >= 1.7) { + return "C-"; + } else if (gpa >= 1.3) { + return "D+"; + } else if (gpa >= 1.0) { + return "D"; + } else if (gpa < 1.0 && gpa >= 0) { + return "F"; + } else { + return "Invalid"; + } } -/** +const grade = convertGpaToLetterGrade(3.2); +console.log(grade); + +/** (Q4) * Computes the factorial of the given value of `n`. * * @param n The value for which to compute the factorial. * @return The factorial of n. */ export function computeFactorial(n: number): number { - return 0; + let result = 1; + for (let i = 1; i <= n; i++) { + + result *= i; + } + return result; } -/** +const n = 7; +console.log(computeFactorial(n)); + +/** (Q5) * Adds all of the provided values and returns the sum. * * @param values The values to sum. * @return The sum of all the values. */ export function addNumbers(values: number[]): number { - return 0; + let total = 0; + for (const value of values) { + total += value; + } + return total; } -/** +const numbers = [8, 4, 6, 2, 7]; +const sum = addNumbers(numbers); +console.log(sum); + +/** (Q6) * Returns an array of the first `n` Fibonacci numbers starting from 1. * * @param n The first `n` of Fibonacci values to compute. * @return An array containing the first `n` Fibonacci values. */ +//** I had to look up how to complete this problem on google export function getFirstNFibonacciNumbers(n: number): number[] { - return []; + const myArray: number[] = []; + if (n <= 0) return []; + if (n === 1) return [1]; + if (n === 2) return [1, 1]; + myArray.push(1, 1); + + for (let i = 2; i < n; i++) { + + const nextNum = myArray[i - 1] + myArray[i - 2]; + myArray.push(nextNum); + } + return myArray; } -/** +console.log(getFirstNFibonacciNumbers(7)); + +/** (Q7) * Finds a value in an array of values. * * @param values The values to search. @@ -86,17 +171,20 @@ 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. + const pivotIndex = Math.floor((start + end) / 2); - // 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); + } + return binarySearch(values, pivotIndex + 1, end, value); } + +const values = [ 2, 4, 6, 8, 10, 12, 14, 16]; +const index = binarySearch(values, 4, 8, 14); +console.log(index); \ No newline at end of file