Skip to content
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=D
33 changes: 29 additions & 4 deletions lesson_07/conditionals/src/lesson7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ export function compareStrings(a: string, b: string): number {
// if it is greater, and 0 if the strings are equal.
const distance = computeLexicographicDistance(a, b);

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

return 0;
}
Expand All @@ -24,7 +28,15 @@ export function compareStrings(a: string, b: string): number {
* @return The factorial of n.
*/
export function computeFactorial(n: number): number {
return 0;
if (n < 0) return 0;
if (n === 0) return 1;

let result = n;
for (let i = n - 1; i >= 1; i--) {
result *= i;
}

return result;
}

/**
Expand All @@ -34,7 +46,16 @@ 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 [];
if (n === 1) return [1];
if (n === 2) return [1, 1];

const fibArr: number[] = [1, 1];
for (let i = 2; i < n; i++) {
fibArr.push(fibArr[i - 1] + fibArr[i - 2]);
}

return fibArr;
}

/**
Expand Down Expand Up @@ -65,5 +86,9 @@ 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);
}
14 changes: 12 additions & 2 deletions lesson_07/conditionals/src/part_d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @returns
*/
export function isWithinRange(num: number, min: number, max: number): boolean {
return false;
return num >= min && num <= max;
}

/**
Expand All @@ -19,7 +19,7 @@ export function isWithinRange(num: number, min: number, max: number): boolean {
* @returns
*/
export function isValidTriangle(a: number, b: number, c: number): boolean {
return false;
return Math.pow(a, 2) + Math.pow(b, 2) === Math.pow(c, 2);
}

/**
Expand All @@ -30,5 +30,15 @@ export function isValidTriangle(a: number, b: number, c: number): boolean {
* @returns
*/
export function getSeason(month: number): string {
if (month >= 3 && month <= 5) {
return "Spring";
} else if (month >= 6 && month <= 8) {
return "Summer";
} else if (month >= 9 && month <= 11) {
return "Fall";
} else if (month === 12 || month === 1 || month === 2) {
return "Winter";
}

return "Invalid month";
}