diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index 52f45df8b..920c50a4e 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,6 +25,12 @@ 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) { + // instructions say 'distance will be A NUMBER less than 0 ' not quite exactly -1 + return -1; + } + + return distance; // instructions clearly state will be 0 if a === b and 1 if a > b which is what we want // TODO(you): Finish this method. return 0; @@ -37,7 +46,34 @@ 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"; + console.log(gpa); + switch ( + true // better way to do this. want to use the equivilent of a Java Map (XavierCruz5106) + ) { + case gpa >= 1.0 && gpa < 1.3: + return "D"; + case gpa >= 1.3 && gpa < 1.7: + return "D+"; + case gpa >= 1.7 && gpa < 2.0: + return "C-"; + case gpa >= 2.0 && gpa < 2.3: + return "C"; + case gpa >= 2.3 && gpa < 2.7: + return "C+"; + case gpa >= 2.7 && gpa < 3.0: + return "B-"; + case gpa >= 3.0 && gpa < 3.3: + return "B"; + case gpa >= 3.3 && gpa < 3.7: + return "B+"; + case gpa >= 3.7 && gpa < 4.0: + return "A-"; + case gpa === 4.0: + return "A"; + + default: + return "F"; + } } /** @@ -47,7 +83,12 @@ export function convertGpaToLetterGrade(gpa: number): string { * @return The factorial of n. */ export function computeFactorial(n: number): number { - return 0; + let total = 1; + for (let i = 2; i <= n; i++) { + total *= i; + } + + return total; } /** @@ -57,7 +98,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; + values.forEach((value) => { + sum += value; + }); + return sum; } /** @@ -67,7 +112,17 @@ export function addNumbers(values: number[]): number { * @return An array containing the first `n` Fibonacci values. */ export function getFirstNFibonacciNumbers(n: number): number[] { - return []; + let current = 1; + let prev = 0; + const nums = []; + + for (let i = 1; i <= n; i++) { + nums.push(current); + const nextNum = current + prev; + prev = current; + current = nextNum; + } + return nums; } /** @@ -94,9 +149,16 @@ 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; }