Skip to content

feat: adds Shawn Dunsmore Lesson07 #277

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
99 changes: 86 additions & 13 deletions lesson_07/conditionals/src/lesson7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 false;
if (age >= 18) return true;

return true;
}

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

// TODO(you): Finish this method.
if (distance < 0) {
return -1;
}

return 0;
return distance;
}

/**
Expand All @@ -37,6 +43,25 @@ export function compareStrings(a: string, b: string): number {
* @return The letter grade ("A+", "A", "A-", "B+", etc.).
*/
export function convertGpaToLetterGrade(gpa: number): string {
if (gpa >= 4.0) return "A";
if (gpa >= 3.99) return "A-";
if (gpa >= 3.7) return "A-";
if (gpa >= 3.69) return "B+";
if (gpa >= 3.3) return "B+";
if (gpa >= 3.29) return "B";
if (gpa >= 3.0) return "B";
if (gpa >= 2.99) return "B-";
if (gpa >= 2.7) return "B-";
if (gpa >= 2.69) return "C+";
if (gpa >= 2.3) return "C+";
if (gpa >= 2.29) return "C";
if (gpa >= 2.0) return "C";
if (gpa >= 1.99) return "C-";
if (gpa >= 1.7) return "C-";
if (gpa >= 1.69) return "D+";
if (gpa >= 1.3) return "D+";
if (gpa >= 1.29) return "D";
if (gpa >= 1.0) return "D";
return "F";
}

Expand All @@ -47,7 +72,12 @@ export function convertGpaToLetterGrade(gpa: number): string {
* @return The factorial of n.
*/
export function computeFactorial(n: number): number {
return 0;
if (n === 1 || n === 0) return 1;

let nums = 1;
for (let i = 2; i <= n; i++) nums *= i;

return nums;
}

/**
Expand All @@ -57,7 +87,13 @@ 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 += value;
}

return sum;
}

/**
Expand All @@ -67,7 +103,23 @@ export function addNumbers(values: number[]): number {
* @return An array containing the first `n` Fibonacci values.
*/
export function getFirstNFibonacciNumbers(n: number): number[] {
return [];
const fibNumbers: number[] = [];

if (n <= 0) return fibNumbers;

let first = 1,
second = 1;
fibNumbers.push(first);
if (n > 1) fibNumbers.push(second);

for (let i = 2; i < n; i++) {
const next = first + second;
fibNumbers.push(next);
first = second;
second = next;
}

return fibNumbers;
}

/**
Expand All @@ -83,20 +135,41 @@ export function binarySearch(
values: number[],
start: number,
end: number,
value: number,
target: number,
): number {
if (end < start) {
// The range is not valid so just return -1.
if (start > end) {
return -1;
}

const mid = Math.floor((start + end) / 2);

if (values[mid] === target) {
return mid;
} else if (values[mid] > target) {
return binarySearch(values, start, mid - 1, target);
} else {
return binarySearch(values, mid + 1, end, target);
}

/**
* The following code is part of the binary search logic:
*/

const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.

// TODO(you): Finish implementing this algorithm
if (values[pivotIndex] === target) {
return pivotIndex;
} else if (values[pivotIndex] > target) {
return binarySearch(values, start, pivotIndex - 1, target);
} else {
return binarySearch(values, pivotIndex + 1, end, target);
}

// 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;
}
// TODO(you): Finish implementing this algorithm

// 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.