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=E
35 changes: 29 additions & 6 deletions lesson_07/conditionals/src/lesson7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ 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;
return distance < 0 ? -1 : distance > 0 ? 1 : 0;
}

/**
Expand All @@ -24,7 +22,16 @@ export function compareStrings(a: string, b: string): number {
* @return The factorial of n.
*/
export function computeFactorial(n: number): number {
return 0;
// TODO(you): Finish this method.
if (n < 0) {
return 0;
}

let answer = 1;
for (let i = 1; i <= n; i++) {
answer *= i;
}
return answer;
}

/**
Expand All @@ -34,7 +41,17 @@ 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 [];
if (n === 1) return [1];
if (n === 2) return [1, 1];

const fibonacci: number[] = [1, 1];

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

return fibonacci;
}

/**
Expand Down Expand Up @@ -65,5 +82,11 @@ export function binarySearch(
// 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);
}
}
18 changes: 15 additions & 3 deletions lesson_07/conditionals/src/part_e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
* @returns
*/
export function isUppercase(char: string): boolean {
return false;
if (char.length !== 1) {
return false;
}
return char === char.toUpperCase() && char !== char.toLowerCase();
}

/**
Expand All @@ -17,7 +20,12 @@ export function isUppercase(char: string): boolean {
* @returns
*/
export function canGetDriverLicense(age: number, passedTest: boolean): boolean {
return false;
if (age >= 18 && passedTest) {
return true;
}
else{
return false;
}
}

/**
Expand All @@ -29,5 +37,9 @@ export function canGetDriverLicense(age: number, passedTest: boolean): boolean {
* @returns
*/
export function isStoreOpen(day: string, hour: number): boolean {
return false;
if (day === "Sunday") {
return false;
}
return hour > 9 && hour < 21;
}