-
Notifications
You must be signed in to change notification settings - Fork 25
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
XavierCruz5106
wants to merge
3
commits into
code-differently:main
from
XavierCruz5106:feature/lesson_07
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
} | ||
|
||
|
@@ -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; | ||
|
@@ -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) | ||
) { | ||
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"; | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -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; | ||
} | ||
|
||
/** | ||
|
@@ -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) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be better to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks ill look into .reduce!! |
||
sum += value; | ||
}); | ||
return sum; | ||
} | ||
|
||
/** | ||
|
@@ -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; | ||
} | ||
|
||
/** | ||
|
@@ -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; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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