Skip to content

Finished Lesson_07 HW - Xavier Cruz #264

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
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
72 changes: 67 additions & 5 deletions lesson_07/conditionals/src/lesson7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { computeLexicographicDistance } from "./util.js";
* @return True if the age corresponds to a voting age and false otherwise.
*/
export function canVote(age: number): boolean {
if (age >= 18) {
return true;
}
return false;
}

Expand All @@ -22,6 +25,12 @@ export function compareStrings(a: string, b: string): number {
// if it is greater, and 0 if the strings are equal.
const distance = computeLexicographicDistance(a, b);

if (distance < 0) {
// instructions say 'distance will be A NUMBER less than 0 ' not quite exactly -1
return -1;
}

return distance; // instructions clearly state will be 0 if a === b and 1 if a > b which is what we want
// TODO(you): Finish this method.

return 0;
Expand All @@ -37,7 +46,34 @@ 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";
console.log(gpa);
switch (
true // better way to do this. want to use the equivilent of a Java Map (XavierCruz5106)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting solution, though I wouldn't argue this is better than just using if statements (need a couple more lines of code for this construction). Nice though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it was the first solution that came to my head. the second was to use something like a java esque map of some sort and do a lookup instead of a comparison but im not sure how maps work in typescript so ive stray away from that approach

) {
case gpa >= 1.0 && gpa < 1.3:
return "D";
case gpa >= 1.3 && gpa < 1.7:
return "D+";
case gpa >= 1.7 && gpa < 2.0:
return "C-";
case gpa >= 2.0 && gpa < 2.3:
return "C";
case gpa >= 2.3 && gpa < 2.7:
return "C+";
case gpa >= 2.7 && gpa < 3.0:
return "B-";
case gpa >= 3.0 && gpa < 3.3:
return "B";
case gpa >= 3.3 && gpa < 3.7:
return "B+";
case gpa >= 3.7 && gpa < 4.0:
return "A-";
case gpa === 4.0:
return "A";

default:
return "F";
}
}

/**
Expand All @@ -47,7 +83,12 @@ export function convertGpaToLetterGrade(gpa: number): string {
* @return The factorial of n.
*/
export function computeFactorial(n: number): number {
return 0;
let total = 1;
for (let i = 2; i <= n; i++) {
total *= i;
}

return total;
}

/**
Expand All @@ -57,7 +98,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;
values.forEach((value) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be better to use values.reduce. Know how to use it, comes in handy especially for interview prep but also IRL.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks ill look into .reduce!!

sum += value;
});
return sum;
}

/**
Expand All @@ -67,7 +112,17 @@ export function addNumbers(values: number[]): number {
* @return An array containing the first `n` Fibonacci values.
*/
export function getFirstNFibonacciNumbers(n: number): number[] {
return [];
let current = 1;
let prev = 0;
const nums = [];

for (let i = 1; i <= n; i++) {
nums.push(current);
const nextNum = current + prev;
prev = current;
current = nextNum;
}
return nums;
}

/**
Expand All @@ -94,9 +149,16 @@ export function binarySearch(

// TODO(you): Finish implementing this algorithm

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);
}

// 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;
}