diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index 52f45df8b..07b25a2ff 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -7,9 +7,13 @@ 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 true; + } else { + return false; + } +} /** * Compares two strings lexicographically. * @@ -24,7 +28,13 @@ export function compareStrings(a: string, b: string): number { // TODO(you): Finish this method. - return 0; + if(distance < 0){ + return -1; + } if(distance > 0){ + return 1; + }else{ + return 0; + } } /** @@ -37,6 +47,28 @@ 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"; + } 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"; + } return "F"; } @@ -47,7 +79,13 @@ export function convertGpaToLetterGrade(gpa: number): string { * @return The factorial of n. */ export function computeFactorial(n: number): number { - return 0; + + let totalValue = 1 + + for(let i = 1; n >= i; i++){ + totalValue *= i + } + return totalValue; } /** @@ -57,7 +95,14 @@ 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 i of values){ + sum += i; + } + + return sum; } /** @@ -67,7 +112,19 @@ export function addNumbers(values: number[]): number { * @return An array containing the first `n` Fibonacci values. */ export function getFirstNFibonacciNumbers(n: number): number[] { - return []; + + const array = [1, 1] + + if (n <= 0){ + return []; + }else if(n === 1){ + return [1] + } + for(let i = 2; i < n; i++){ + const cont = array[i - 1] + array[i - 2]; + array.push(cont) + } + return array; } /** @@ -93,10 +150,15 @@ 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; + } 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; }