Skip to content

feat: created new branch and completed lesson #369

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
wants to merge 1 commit into from
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
50 changes: 41 additions & 9 deletions lesson_07/conditionals/src/lesson7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ 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);
return distance;
}

// TODO(you): Finish this method.
// TODO(you): Finish this method.

return 0;
}
// Removed misplaced return statement

/**
* Computes the factorial of the given value of `n`.
Expand All @@ -24,7 +25,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;
}
if (n === 1) {
return 1;
}
if (n === 2) {
return 2;
}
let result = 1;
for (let i = 2; i <= n; ++i) {
result *= i;
}
return result;
}

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

/**
Expand All @@ -56,14 +83,19 @@ export function binarySearch(
// The range is not valid so just return -1.
return -1;
}

const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.

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

const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.
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);
}
}
35 changes: 32 additions & 3 deletions lesson_07/conditionals/src/part_b.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@
* @returns
*/
export function isLeapYear(year: number): boolean {
return false;
if (year % 400 === 0) {
return true;
} else if (year % 100 === 0) {
return false;
} else if (year % 4 === 0) {
return true;
} else {
return false;
}
}

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

/**
Expand All @@ -25,5 +37,22 @@ export function isEvenOrOdd(num: number): string {
* @returns
*/
export function hasVowel(word: string): boolean {
return false;
const lowerCaseword = word.toLowerCase();
if (lowerCaseword.includes("a")) {
return true;
}
if (lowerCaseword.includes("e")) {
return true;
}
if (lowerCaseword.includes("i")) {
return true;
}
if (lowerCaseword.includes("o")) {
return true;
}
if (lowerCaseword.includes("u")) {
return true;
} else {
return false;
}
}