diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index 52f45df8b..9194ac19e 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -7,7 +7,12 @@ 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; + } } /** @@ -21,10 +26,15 @@ 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); - - // TODO(you): Finish this method. - - return 0; + if (distance>0){ + return 1; + } + if (distance<0){ + return -1 + } + else{ + return 0; + } } /** @@ -32,12 +42,70 @@ export function compareStrings(a: string, b: string): number { * scale. See * https://bigfuture.collegeboard.org/plan-for-college/college-basics/how-to-convert-gpa-4.0-scale * for details. - * + * A+ 97-100 4.0 + A 93-96 4.0 + A- 90-92 3.7 + B+ 87-89 3.3 + B 83-86 3.0 + B- 80-82 2.7 + C+ 77-79 2.3 + C 73-76 2.0 + C- 70-72 1.7 + D+ 67-69 1.3 + D 65-66 1.0 + E/F Below 65 0.0 * @param gpa The GPA value. * @return The letter grade ("A+", "A", "A-", "B+", etc.). */ export function convertGpaToLetterGrade(gpa: number): string { - return "F"; + let letterGrade = "N/A"; + if (gpa===4.0){ + letterGrade = "A"; + return letterGrade; + } + else if (gpa>=3.7 && gpa<4.0){ + letterGrade = "A-"; + return letterGrade; + } + else if (gpa>=3.3 && gpa<3.7){ + letterGrade = "B+"; + return letterGrade; + } + else if (gpa>=3.0 && gpa<3.3){ + letterGrade = "B"; + return letterGrade; + } + else if (gpa>=2.7 && gpa<3.0){ + letterGrade = "B-"; + return letterGrade; + } + else if (gpa>=2.3 && gpa<2.7){ + letterGrade = "C+"; + return letterGrade; + } + else if (gpa>=2.0 && gpa<2.3){ + letterGrade = "C"; + return letterGrade; + } + else if (gpa>=1.7 && gpa<2.0){ + letterGrade = "C-"; + return letterGrade; + } + else if (gpa>=1.3 && gpa<1.7){ + letterGrade = "D+"; + return letterGrade; + } + else if (gpa>=1.0 && gpa<1.3){ + letterGrade = "D"; + return letterGrade; + } + else if (gpa>=0 && gpa<1.0){ + letterGrade = "F"; + return letterGrade; + } + else{ + return letterGrade; + } } /** @@ -47,7 +115,15 @@ export function convertGpaToLetterGrade(gpa: number): string { * @return The factorial of n. */ export function computeFactorial(n: number): number { - return 0; + let fact = 1; + if (n>0){ + const numbers = Array.from({ length: n }, (_, index) => index + 1); + for (const i of numbers) { + fact *= i; + } + return fact; + } + return fact; } /** @@ -57,7 +133,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; + if (values.length>0){ + for (const value of values) { + sum += value; +} + return sum; + } + return sum; } /** @@ -67,7 +150,20 @@ export function addNumbers(values: number[]): number { * @return An array containing the first `n` Fibonacci values. */ export function getFirstNFibonacciNumbers(n: number): number[] { - return []; + const list: number[] = []; + const indices = Array.from({ length: n - 2 }, (_, index) => index + 2); + if (n>=1){ + list.push(1); + } + if (n>=2){ + list.push(1); + } + if (n>=3){ + for (const i of indices) { + list[i] = list[i - 1] + list[i - 2]; + } + } + return list; } /** @@ -98,5 +194,13 @@ export function binarySearch( // 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; + 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); + } }