Skip to content

Feat: adds lesson_07 assignment for Tommy Tran #279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 86 additions & 21 deletions lesson_07/conditionals/src/lesson7.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
import { computeLexicographicDistance } from "./util.js";

/**
* Returns true if the provided age meets the minimum US voting age and false otherwise.
*
* @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 {
if (age >= 18) {
console.log("You can Vote");
return true;
} else console.log("you cannot vote");
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
// 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 (a === b) {
return 0;
} else if (a > b) {
return 1;
} else return -1;
}

/**
Expand All @@ -37,17 +41,50 @@ 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";
switch (true) {
case gpa >= 4.0:
return "A";
case gpa >= 3.7 && gpa <= 3.99:
return "A-";
case gpa >= 3.3 && gpa <= 3.69:
return "B+";
case gpa >= 3 && gpa <= 3.29:
return "B";
case gpa >= 2.7 && gpa <= 2.99:
return "B-";
case gpa >= 2.3 && gpa <= 2.69:
return "C+";
case gpa >= 2 && gpa <= 2.29:
return "C";
case gpa >= 1.7 && gpa <= 1.99:
return "C-";
case gpa >= 1.3 && gpa <= 1.69:
return "D+";
case gpa >= 1 && gpa <= 1.29:
return "D";
default:
return "F";
}
}

/**
* Computes the factorial of the given value of `n`.
* Computes the factorial of the given value of n.
*
* @param n The value for which to compute the factorial.
* @return The factorial of n.
*/
export function computeFactorial(n: number): number {
return 0;
if (n === 1 || n === 0) {
return 1;
} else {
let factorial = 1; //initialize variable
for (
let i = 2;
i <= n;
i++ // i starts at 2 and increments until it is equal to n
)
factorial = factorial * i; // factorial loops by multiplying with i until it = n
return factorial;
}
}

/**
Expand All @@ -57,17 +94,42 @@ export function computeFactorial(n: number): number {
* @return The sum of all the values.
*/
export function addNumbers(values: number[]): number {
return 0;
let sum = 0; // initialize variable

// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (let i = 0; i < values.length; i++) {
// loop starting 0 as long as i is less than the length of values array increment
sum = sum + values[i]; // add the values in the array;
}
return sum;
}

/**
* Returns an array of the first `n` Fibonacci numbers starting from 1.
* Returns an array of the first n Fibonacci numbers starting from 1.
*
* @param n The first `n` of Fibonacci values to compute.
* @return An array containing the first `n` Fibonacci values.
* @param n The first n of Fibonacci values to compute.
* @return An array containing the first n Fibonacci values.
*/
export function getFirstNFibonacciNumbers(n: number): number[] {
return [];
const array: number[] = [];

if (n === 0) {
return array;
}

if (n >= 1) {
array.push(1); // Add the first Fibonacci number
}
if (n >= 2) {
array.push(1);
}
// starting at 2 increment i until it is = n
for (let i = 2; i < n; i++) {
const Fibonacci = array[i - 1] + array[i - 2]; //add the numbers 2 positions behind to get current position
array.push(Fibonacci);
}

return array;
}

/**
Expand All @@ -94,9 +156,12 @@ export function binarySearch(

// TODO(you): Finish implementing this algorithm

// If values[pivotIndex] is equal to value then return `pivotIndex`.
// 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;
// call binarySearch(values, start, pivotIndex - 1, value) and return its value;
// Else call binarySearch(values, pivotIndex + 1, end, value) and return its value.
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);
}