diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index 52f45df8b..80bc44679 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -7,24 +7,30 @@ 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. * - * @param a The first `string` to compare. - * @param b The second `string` to compare. + * @param a The first string to compare. + * @param b The second string to compare. * @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. + // The distance will be a number less than 0 if string a is lexicographically less than b, + // greater than 0 if a is greater, and 0 if the strings are equal. const distance = computeLexicographicDistance(a, b); - // TODO(you): Finish this method. - - return 0; + if (distance < 0) { + return -1; + } else if (distance > 0) { + return 1; + } else { + return 0; + } } /** @@ -37,7 +43,27 @@ 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) { + 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 return "F"; } /** @@ -47,7 +73,10 @@ export function convertGpaToLetterGrade(gpa: number): string { * @return The factorial of n. */ export function computeFactorial(n: number): number { - return 0; + if (n < 0) { + throw new Error("Negative numbers are invalid."); + } + return n <= 1 ? 1 : n * computeFactorial(n - 1); } /** @@ -57,17 +86,27 @@ export function computeFactorial(n: number): number { * @return The sum of all the values. */ export function addNumbers(values: number[]): number { - return 0; + return values.reduce((sum, current) => sum + current, 0); } /** * Returns an array of the first `n` Fibonacci numbers starting from 1. * - * @param n The first `n` of Fibonacci values to compute. + * @param n The first `n` Fibonacci values to compute. * @return An array containing the first `n` Fibonacci values. */ export function getFirstNFibonacciNumbers(n: number): number[] { - return []; + if (n <= 0) return []; + if (n === 1) return [1]; + + const fibonacci: number[] = [1, 1]; + + for (let i = 2; i < n; i++) { + const nextFibonacci = fibonacci[i - 1] + fibonacci[i - 2]; + fibonacci.push(nextFibonacci); + } + + return fibonacci; } /** @@ -90,13 +129,12 @@ export function binarySearch( return -1; } - const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array. - - // 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; + const pivotIndex = Math.floor((start + end) / 2); + 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); + } }