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

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

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

Choose a reason for hiding this comment

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

Your tests are not passing, please fix your computeFactorial implementation

return 1;
}
return n * computeFactorial(n - 1);
}

/**
Expand All @@ -34,7 +41,21 @@ 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 fibonacci: number[] = [1];
if (n === 1) {
return fibonacci;
}

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

return fibonacci;
}

/**
Expand All @@ -59,11 +80,11 @@ 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] 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;
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);
}
}
66 changes: 60 additions & 6 deletions lesson_07/conditionals/src/part_c.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,64 @@
* and at least one uppercase letter and one digit.
*
* @param password
* @returns
* @returns true if strong, false otherwise
*/
export function isStrongPassword(password: string): boolean {
return false;
// Check length
if (password.length < 8) {
return false;
}

let hasUppercase = false;
let hasDigit = false;

// Check for uppercase and digits in one pass through the string
for (let i = 0; i < password.length; i++) {
const ch = password[i];

// Check uppercase
if (ch >= "A" && ch <= "Z") {
hasUppercase = true;
}

// Check digit
if (ch >= "0" && ch <= "9") {
hasDigit = true;
}
}

// Strong only if all conditions are met
if (hasUppercase && hasDigit) {
return true;
} else {
return false;
}
}

/**
* Determines the day of the week on the given 0-based number.
*
* @param day
* @returns
* @returns The day name or "Invalid day" if out of range.
*/
export function getDayOfWeek(day: number): string {
return "";
if (day === 0) {
return "Sunday";
} else if (day === 1) {
return "Monday";
} else if (day === 2) {
return "Tuesday";
} else if (day === 3) {
return "Wednesday";
} else if (day === 4) {
return "Thursday";
} else if (day === 5) {
return "Friday";
} else if (day === 6) {
return "Saturday";
} else {
return "";
}
}

/**
Expand All @@ -28,8 +72,18 @@ export function getDayOfWeek(day: number): string {
* - Seniors 60 years old and older pay 15 dollars.
*
* @param age
* @returns
* @returns Ticket price in dollars.
*/
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 if (age >= 60) {
return 15;
} else {
return 0;
}
}
Loading