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= G
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There shouldn't be a space before the G.

48 changes: 41 additions & 7 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,19 @@ 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 = 1;
for (let i = 1; 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 <= 0) {
return []; // Return an empty array for non-positive lengths
} else if (n === 1) {
return [1]; // The first Fibonacci number is 1 (as per the request)
}

const fibArray: number[] = [1, 1]; // Initialize with the first two Fibonacci numbers starting from 1

for (let i = 2; i < n; i++) {
const nextFib = fibArray[i - 1] + fibArray[i - 2];
fibArray.push(nextFib);
}

return fibArray;
}

/**
Expand All @@ -46,6 +73,7 @@ export function getFirstNFibonacciNumbers(n: number): number[] {
* @param value The value to look for.
* @return The index of the value if found in the array and -1 otherwise.
*/

export function binarySearch(
values: number[],
start: number,
Expand All @@ -57,8 +85,14 @@ export function binarySearch(
return -1;
}

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

const pivotIndex = Math.floor((start + end) / 2);
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);
}
// TODO(you): Finish implementing this algorithm

// If values[pivotIndex] is equal to value then return `pivotIndex`.
Expand Down
41 changes: 38 additions & 3 deletions lesson_07/conditionals/src/part_g.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@
* @returns
*/
export function findLargestNumber(numbers: number[]): number {
return 0;
if (numbers.length === 0) {
return 0;
}

let largest = numbers[0];
for (let i = 1; i < numbers.length; i++) {
if (numbers[i] > largest) {
largest = numbers[i];
}
}
return largest;
}

/**
Expand All @@ -16,6 +26,10 @@ export function findLargestNumber(numbers: number[]): number {
* @returns
*/
export function isPalindrome(text: string): boolean {
const cleaned = text.replace(/[^a-zA-Z0-9]/g, "").toLowerCase();

return cleaned === cleaned.split("").reverse().join("");

return false;
}

Expand All @@ -34,7 +48,28 @@ export function daysUntilBirthday(
currentMonth: number,
currentDay: number,
birthMonth: number,
birthDay: number
birthDay: number,
): number {
return 0;
const today = new Date();
const currentYear = today.getFullYear();

// Create a date for the birthday this year
let nextBirthday = new Date(currentYear, birthMonth - 1, birthDay);

// If the birthday has already passed this year, set it for next year
if (
birthMonth < currentMonth ||
(birthMonth === currentMonth && birthDay < currentDay)
) {
nextBirthday = new Date(currentYear + 1, birthMonth - 1, birthDay);
}

const currentDate = new Date(currentYear, currentMonth - 1, currentDay);

// Calculate the difference in milliseconds and convert to days
const diffTime = nextBirthday.getTime() - currentDate.getTime();
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));

return diffDays;
}