diff --git a/lesson_07/conditionals/src/lesson7.ts b/lesson_07/conditionals/src/lesson7.ts index 52f45df8b..d412bcf0e 100644 --- a/lesson_07/conditionals/src/lesson7.ts +++ b/lesson_07/conditionals/src/lesson7.ts @@ -7,7 +7,10 @@ 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 +24,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); + if(distance === 0 ) + return 0; + else if (distance > 0) + return 1; + else + return -1; // TODO(you): Finish this method. - - return 0; + // return 0; } /** @@ -37,7 +45,28 @@ 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.0) + return "A"; + else if (gpa >= 3.7 && gpa <= 3.99) + return "A-"; + else if(gpa >= 3.3 && gpa <= 3.69) + return "B+"; + else if(gpa >= 3.0 && gpa <= 3.29) + return "B"; + else if(gpa >= 2.7 && gpa <= 2.99) + return "B-"; + else if(gpa>=2.3 && gpa <= 2.69) + return "C+"; + else if(gpa >= 2.0 && gpa <= 2.29) + return "C"; + else if(gpa >= 1.7 && gpa <= 1.99) + return "C-"; + else if(gpa >= 1.3 && gpa <= 1.69) + return "D+"; + else if(gpa >= 1 && gpa <= 1.29) + return "D"; + else //(gpa === 0.99) + return "F"; } /** @@ -47,7 +76,12 @@ export function convertGpaToLetterGrade(gpa: number): string { * @return The factorial of n. */ export function computeFactorial(n: number): number { - return 0; + if(n === 0) { + return 1; + } + else{ + return n * computeFactorial(n-1); + } } /** @@ -57,7 +91,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(const value of values){ + sum = sum + value; + } + return sum; } /** @@ -67,7 +105,20 @@ export function addNumbers(values: number[]): number { * @return An array containing the first `n` Fibonacci values. */ export function getFirstNFibonacciNumbers(n: number): number[] { - return []; + const resultArray :number[] =[]; + if( n == 0) + return resultArray; + resultArray.push(1); + let firstNumber = 1; + let secondNumber = 1; + let nextNumber = 0; + for(let i = 1; i < n; i++ ){ + nextNumber = firstNumber + secondNumber; + resultArray.push(secondNumber); + firstNumber = secondNumber; + secondNumber = nextNumber; + } + return resultArray; } /** @@ -93,10 +144,20 @@ 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; + } + else if (values[pivotIndex] > value){ + const posGreaterThanPivotIndex = binarySearch(values, start, pivotIndex - 1, value); + return posGreaterThanPivotIndex; + } + else{ + const posLessThanPivotIndex = binarySearch(values, pivotIndex + 1, end, value); + return posLessThanPivotIndex; + } // 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; + //return -1; }