Skip to content

feat: Rmiller_lesson_07_homework #320

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
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion lesson_07/conditionals/.env.test
Original file line number Diff line number Diff line change
@@ -1 +1 @@
HW_VERSION=your homework version here
HW_VERSION=A
49 changes: 38 additions & 11 deletions lesson_07/conditionals/src/lesson7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,31 @@ export function compareStrings(a: string, b: string): number {
const distance = computeLexicographicDistance(a, b);

// TODO(you): Finish this method.

if (distance < 0) return -1;
if (distance > 0) return 1;
return 0;
}

/**
* Computes the factorial of the given value of `n`.
*
* @param n The value for which to compute the factorial.
* @return The factorial of n.
* @return The factorial of n, or 0 if n is negative.
*/
export function computeFactorial(n: number): number {
return 0;
if (n < 0) {
return 0; // Return 0 for negative numbers as per the test case
}
if (n === 0 || n === 1) {
return 1;
}

let result = 1;
for (let i = 2; i <= n; i++) {
result *= i;
}

return result;
}

/**
Expand All @@ -34,9 +47,17 @@ export function computeFactorial(n: number): number {
* @return An array containing the first `n` Fibonacci values.
*/
export function getFirstNFibonacciNumbers(n: number): number[] {
return [];
if (n===0) {
return []
}
const fibNum = [1, 1];
for (let i = 2; i < n; i++) {
fibNum.push(fibNum[i - 1] + fibNum[i - 2])
}
return fibNum;
}


/**
* Finds a value in an array of values.
*
Expand All @@ -58,12 +79,18 @@ export function binarySearch(
}

const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.
if (values[pivotIndex] === value) {
return pivotIndex; // Value found at pivot index.
} else if (values[pivotIndex] > value) {
return binarySearch(values, start, pivotIndex - 1, value); // Search left half.
} else {
return binarySearch(values, pivotIndex + 1, end, value); // Search right half.
}
}

// TODO(you): Finish implementing this algorithm
// 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.
return -1;
}
// 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.
10 changes: 6 additions & 4 deletions lesson_07/conditionals/src/part_a.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@
* @return True if the age corresponds to a voting age and false otherwise.
*/
export function canVote(age: number): boolean {
return false;
return age >= 18;
}

/**
* Adds all of the provided values and returns the sum.
*
* @param values The values to sum.
* @return The sum of all the values.
*/
export function addNumbers(values: number[]): number {
return 0;
return values.reduce((sum, num) => sum + num, 0);
}

/**
Expand All @@ -25,5 +24,8 @@ export function addNumbers(values: number[]): number {
* @return The factorial of n.
*/
export function computeFactorial(n: number): number {
return 0;
if (n < 0) {
throw new Error("Factorial is not defined for negative numbers.");
}
return n <= 1 ? 1 : n * computeFactorial(n - 1);
}