From 382b75aba9b00f2a3969fac4d411fa16856b9ee6 Mon Sep 17 00:00:00 2001 From: cdbluejr Date: Mon, 14 Oct 2024 16:40:31 +0000 Subject: [PATCH] feat: Lesson 7 Homework - Partial Author:cdbluejr --- lesson_07/conditionals/src/lesson7.ts | 55 ++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 6 deletions(-) diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index 52f45df8b..ffa69c472 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -6,10 +6,14 @@ import { computeLexicographicDistance } from "./util.js"; * @param age The age to check. * @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. * @@ -21,7 +25,13 @@ 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. 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 +47,31 @@ 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"; + let letterGrade = ""; + if (gpa > 3.99) { + letterGrade = "A"; + } else if (gpa > 3.69 && gpa < 4) { + letterGrade = "A-"; + } else if (gpa > 3.29 && gpa < 3.7) { + letterGrade = "B+"; + } else if (gpa > 2.99 && gpa < 3.3) { + letterGrade = "B"; + } else if (gpa > 2.69 && gpa < 3.0) { + letterGrade = "B-"; + } else if (gpa > 2.29 && gpa < 2.7) { + letterGrade = "C+"; + } else if (gpa > 1.99 && gpa < 2.3) { + letterGrade = "C"; + } else if (gpa > 1.69 && gpa < 2.0) { + letterGrade = "C-"; + } else if (gpa > 1.29 && gpa < 1.7) { + letterGrade = "D+"; + } else if (gpa > 0.99 && gpa < 1.3) { + letterGrade = "D"; + } else if (gpa < 1) { + letterGrade = "F"; + } + return letterGrade; } /** @@ -47,7 +81,12 @@ export function convertGpaToLetterGrade(gpa: number): string { * @return The factorial of n. */ export function computeFactorial(n: number): number { - return 0; + let integer; + let factorial = 1; + for (integer = n; integer > 1; integer--) { + factorial *= integer; + } + return factorial; } /** @@ -57,7 +96,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 (let x = 0; values.length > x; x++) { + sum += values[x]; + } + return sum; } /**