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=B
45 changes: 39 additions & 6 deletions lesson_07/conditionals/src/lesson7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ export function compareStrings(a: string, b: string): number {
// The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
// 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 +26,20 @@ 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;
} else if (n === 0) {
return 1;
} else if (n <= 1) {
return 1;
}
let formula = 1;
for (let i = n; i >= 1; i--) {
formula = formula * i;

//5! = 5 x 4 -- 20 x 3 -- 60 x 2 -- 120 x 1
}
return formula;
}

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

/**
Expand All @@ -60,7 +88,12 @@ export function binarySearch(
const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.

// TODO(you): Finish implementing this algorithm

if (values[pivotIndex] === value) {
return pivotIndex;
} else if (values[pivotIndex] > value) {
return binarySearch(values, start, pivotIndex - 1, value);
}
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;
Expand Down
19 changes: 19 additions & 0 deletions lesson_07/conditionals/src/part_b.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
* @returns
*/
export function isLeapYear(year: number): boolean {
if (year % 100 === 0 && year % 400 !== 0) {
return false;
} else if (year % 4 === 0) {
return true;
}
return false;
}

Expand All @@ -15,6 +20,11 @@ export function isLeapYear(year: number): boolean {
* @returns
*/
export function isEvenOrOdd(num: number): string {
if (num % 2 === 0) {
return "even";
} else if (num % 2 !== 0) {
return "odd";
}
return "";
}

Expand All @@ -25,5 +35,14 @@ export function isEvenOrOdd(num: number): string {
* @returns
*/
export function hasVowel(word: string): boolean {
if (
word.includes("a") ||
word.includes("e") ||
word.includes("o") ||
word.includes("i") ||
word.includes("u")
) {
return true;
}
return false;
}