From 72adf4181c7db6fab0e2b2a39160e271a2f8c05f Mon Sep 17 00:00:00 2001 From: Cogobonnia <244cogbonnia@gmail.com> Date: Fri, 11 Oct 2024 13:10:10 +0000 Subject: [PATCH 1/2] fix: completed the functions --- lesson_07/conditionals/src/lesson7.ts | 71 ++++++++++++++++++++++++--- 1 file changed, 63 insertions(+), 8 deletions(-) diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index 52f45df8b..58fd001b2 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -7,6 +7,9 @@ import { computeLexicographicDistance } from "./util.js"; * @return True if the age corresponds to a voting age and false otherwise. */ export function canVote(age: number): boolean { + if (age >= 18) { + return true; + } return false; } @@ -22,9 +25,14 @@ export function compareStrings(a: string, b: string): number { // if it is greater, and 0 if the strings are equal. const distance = computeLexicographicDistance(a, b); + if (distance < 0) { + return -1; + } else if (distance > 0) { + return 1; + } else { + return 0; + } // TODO(you): Finish this method. - - return 0; } /** @@ -37,7 +45,29 @@ 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,14 @@ export function convertGpaToLetterGrade(gpa: number): string { * @return The factorial of n. */ export function computeFactorial(n: number): number { - return 0; + if (n == 0 || n == 1) { + return 1; + } else if (n > 1) { + return n * computeFactorial(n - 1); + } + return n; } - +/* 4! = 4 x 3 x 2 x 1 = 24*/ /** * Adds all of the provided values and returns the sum. * @@ -57,7 +92,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,7 +106,17 @@ export function addNumbers(values: number[]): number { * @return An array containing the first `n` Fibonacci values. */ export function getFirstNFibonacciNumbers(n: number): number[] { - return []; + const fibArray: number[] = []; + let a = 0; + let b = 1; + let c: number; + while (fibArray.length < n) { + c = a; + a = b; + b = c + b; + fibArray.push(a); + } + return fibArray; } /** @@ -94,9 +143,15 @@ export function binarySearch( // TODO(you): Finish implementing this algorithm + 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. - return -1; } From 9cfeec6697fba984359e8f854724cced9c06d978 Mon Sep 17 00:00:00 2001 From: Cogbonnia <244cogbonnia@gmail.com> Date: Fri, 11 Oct 2024 13:17:31 -0400 Subject: [PATCH 2/2] chore: deleted notes and comments --- lesson_07/conditionals/src/lesson7.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index 58fd001b2..3f720b6f1 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -32,7 +32,6 @@ export function compareStrings(a: string, b: string): number { } else { return 0; } - // TODO(you): Finish this method. } /** @@ -84,7 +83,6 @@ export function computeFactorial(n: number): number { } return n; } -/* 4! = 4 x 3 x 2 x 1 = 24*/ /** * Adds all of the provided values and returns the sum. * @@ -141,7 +139,6 @@ 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;