From 265a10734e79dbf10b2649c23bbd715db663f22c Mon Sep 17 00:00:00 2001 From: Sdunsmore2006 Date: Fri, 11 Oct 2024 15:36:03 +0000 Subject: [PATCH 1/2] Working on questions. --- lesson_07/conditionals/src/lesson7.ts | 38 ++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index 52f45df8b..3c5d14f01 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -7,7 +7,10 @@ import { computeLexicographicDistance } from "./util.js"; * @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 false; + if (age >= 18) return true; + + return true; } /** @@ -23,8 +26,11 @@ export function compareStrings(a: string, b: string): number { const distance = computeLexicographicDistance(a, b); // TODO(you): Finish this method. + if (distance < 0) { + return -1; + } - return 0; + return distance; } /** @@ -37,6 +43,18 @@ export function compareStrings(a: string, b: string): number { * @return The letter grade ("A+", "A", "A-", "B+", etc.). */ export function convertGpaToLetterGrade(gpa: number): string { + if (gpa > 4.0) return "A+"; + if (gpa >= 3.7) return "A"; + if (gpa >= 3.3) return "A-"; + if (gpa >= 3.0) return "B+"; + if (gpa >= 2.7) return "B"; + if (gpa >= 2.3) return "B-"; + if (gpa >= 2.0) return "C+"; + if (gpa >= 1.7) return "C"; + if (gpa >= 1.3) return "C-"; + if (gpa >= 1.0) return "D+"; + if (gpa >= 0.0) return "D"; + return "F"; } @@ -47,7 +65,12 @@ export function convertGpaToLetterGrade(gpa: number): string { * @return The factorial of n. */ export function computeFactorial(n: number): number { - return 0; + if (n === 1 || n === 0) return 1; + + let nums = 1; + for (let i = 2; i <= n; i++) nums *= i; + + return nums; } /** @@ -57,7 +80,13 @@ 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; } /** @@ -87,6 +116,7 @@ export function binarySearch( ): number { if (end < start) { // The range is not valid so just return -1. + return -1; } From 92e7a055263f461168d3e581f212d3948f52b425 Mon Sep 17 00:00:00 2001 From: Sdunsmore2006 Date: Sat, 12 Oct 2024 16:04:08 +0000 Subject: [PATCH 2/2] feat: adds Shawn Dunsmore Lesson07 --- lesson_07/conditionals/src/lesson7.ts | 87 ++++++++++++++++++++------- 1 file changed, 65 insertions(+), 22 deletions(-) diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index 3c5d14f01..c5ea52fc8 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -43,18 +43,25 @@ export function compareStrings(a: string, b: string): number { * @return The letter grade ("A+", "A", "A-", "B+", etc.). */ export function convertGpaToLetterGrade(gpa: number): string { - if (gpa > 4.0) return "A+"; - if (gpa >= 3.7) return "A"; - if (gpa >= 3.3) return "A-"; - if (gpa >= 3.0) return "B+"; - if (gpa >= 2.7) return "B"; - if (gpa >= 2.3) return "B-"; - if (gpa >= 2.0) return "C+"; - if (gpa >= 1.7) return "C"; - if (gpa >= 1.3) return "C-"; - if (gpa >= 1.0) return "D+"; - if (gpa >= 0.0) return "D"; - + if (gpa >= 4.0) return "A"; + if (gpa >= 3.99) return "A-"; + if (gpa >= 3.7) return "A-"; + if (gpa >= 3.69) return "B+"; + if (gpa >= 3.3) return "B+"; + if (gpa >= 3.29) return "B"; + if (gpa >= 3.0) return "B"; + if (gpa >= 2.99) return "B-"; + if (gpa >= 2.7) return "B-"; + if (gpa >= 2.69) return "C+"; + if (gpa >= 2.3) return "C+"; + if (gpa >= 2.29) return "C"; + if (gpa >= 2.0) return "C"; + if (gpa >= 1.99) return "C-"; + if (gpa >= 1.7) return "C-"; + if (gpa >= 1.69) return "D+"; + if (gpa >= 1.3) return "D+"; + if (gpa >= 1.29) return "D"; + if (gpa >= 1.0) return "D"; return "F"; } @@ -96,7 +103,23 @@ export function addNumbers(values: number[]): number { * @return An array containing the first `n` Fibonacci values. */ export function getFirstNFibonacciNumbers(n: number): number[] { - return []; + const fibNumbers: number[] = []; + + if (n <= 0) return fibNumbers; + + let first = 1, + second = 1; + fibNumbers.push(first); + if (n > 1) fibNumbers.push(second); + + for (let i = 2; i < n; i++) { + const next = first + second; + fibNumbers.push(next); + first = second; + second = next; + } + + return fibNumbers; } /** @@ -112,21 +135,41 @@ export function binarySearch( values: number[], start: number, end: number, - value: number, + target: number, ): number { - if (end < start) { - // The range is not valid so just return -1. - + if (start > end) { return -1; } + const mid = Math.floor((start + end) / 2); + + if (values[mid] === target) { + return mid; + } else if (values[mid] > target) { + return binarySearch(values, start, mid - 1, target); + } else { + return binarySearch(values, mid + 1, end, target); + } + + /** + * The following code is part of the binary search logic: + */ + const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array. - // TODO(you): Finish implementing this algorithm + if (values[pivotIndex] === target) { + return pivotIndex; + } else if (values[pivotIndex] > target) { + return binarySearch(values, start, pivotIndex - 1, target); + } else { + return binarySearch(values, pivotIndex + 1, end, target); + } - // 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; } +// 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.