Skip to content

chore: adds completed functions for lesson7 and part_c #305

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
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=C
40 changes: 33 additions & 7 deletions lesson_07/conditionals/src/lesson7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ 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.

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

/**
Expand All @@ -24,7 +27,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;
} else {
let result = 1;
for (let i = 2; i <= n; i++) {
result = result * i;
}
return result;
}
}

/**
Expand All @@ -34,7 +45,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 [];
}
const resultsArr: number[] = [1, 1];
if (n > 1) {
for (let i = 2; i < n; i++) {
resultsArr.push(resultsArr[i - 1] + resultsArr[i - 2]);
}
}
return resultsArr;
}

/**
Expand All @@ -60,7 +80,13 @@ 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, pivotIndex + 1, end, value);
} else {
return binarySearch(values, start, pivotIndex - 1, 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
41 changes: 39 additions & 2 deletions lesson_07/conditionals/src/part_c.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
* @returns
*/
export function isStrongPassword(password: string): boolean {
if (password.length >= 8) {
return /\d/.test(password) && /[A-Z]/.test(password);
}
return false;
}

Expand All @@ -16,7 +19,33 @@ export function isStrongPassword(password: string): boolean {
* @returns
*/
export function getDayOfWeek(day: number): string {
return "";
let result = "";
switch (day) {
case 0:
result = "Sunday";
break;
case 1:
result = "Monday";
break;
case 2:
result = "Tuesday";
break;
case 3:
result = "Wednesday";
break;
case 4:
result = "Thursday";
break;
case 5:
result = "Friday";
break;
case 6:
result = "Saturday";
break;
default:
result = "";
}
return result;
}

/**
Expand All @@ -31,5 +60,13 @@ export function getDayOfWeek(day: number): string {
* @returns
*/
export function getTicketPrice(age: number): number {
return 0;
if (age < 5) {
return 0;
} else if (age >= 5 && age <= 17) {
return 10;
} else if (age >= 18 && age <= 59) {
return 20;
} else {
return 15;
}
}