Skip to content

LJ Lesson 7 Pull Request: Populated file with working functions #272

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 1 commit 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
75 changes: 67 additions & 8 deletions lesson_07/conditionals/src/lesson7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ 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;
}
}

/**
Expand All @@ -23,8 +27,13 @@ export function compareStrings(a: string, b: string): number {
const distance = computeLexicographicDistance(a, b);

// TODO(you): Finish this method.

return 0;
let distanceCompareNum = 0;
if (distance < 0) {
distanceCompareNum = -1;
} else if (distance > 0) {
distanceCompareNum = 1;
}
return distanceCompareNum;
}

/**
Expand All @@ -37,7 +46,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 >= 4.0) {
letterGrade = "A";
} else if (gpa < 4.0 && gpa >= 3.7) {
letterGrade = "A-";
} else if (gpa <= 3.69 && gpa >= 3.3) {
letterGrade = "B+";
} else if (gpa <= 3.29 && gpa >= 3.0) {
letterGrade = "B";
} else if (gpa <= 2.99 && gpa >= 2.7) {
letterGrade = "B-";
} else if (gpa <= 2.69 && gpa >= 2.3) {
letterGrade = "C+";
} else if (gpa <= 2.29 && gpa >= 2.0) {
letterGrade = "C";
} else if (gpa <= 1.99 && gpa >= 1.7) {
letterGrade = "C-";
} else if (gpa <= 1.69 && gpa >= 1.3) {
letterGrade = "D+";
} else if (gpa <= 1.29 && gpa >= 1.0) {
letterGrade = "D";
} else if (gpa <= 0.99) {
letterGrade = "F";
}
return letterGrade;
}

/**
Expand All @@ -47,7 +80,11 @@ export function convertGpaToLetterGrade(gpa: number): string {
* @return The factorial of n.
*/
export function computeFactorial(n: number): number {
return 0;
let product = 1;
for (let i = n; i >= 1; i--) {
product *= i;
}
return product;
}

/**
Expand All @@ -57,7 +94,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 i of values) {
sum += i;
}
return sum;
}

/**
Expand All @@ -67,7 +108,19 @@ export function addNumbers(values: number[]): number {
* @return An array containing the first `n` Fibonacci values.
*/
export function getFirstNFibonacciNumbers(n: number): number[] {
return [];
const arr = [];
if (n >= 1) {
arr.push(1);
}
if (n >= 2) {
arr.push(1);
}
if (n >= 3) {
for (let i = 2; i < n; i++) {
arr.push(arr[i - 1] + arr[i - 2]);
}
}
return arr;
}

/**
Expand Down Expand Up @@ -98,5 +151,11 @@ 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);
}
}