Skip to content

Commit 76dd422

Browse files
committed
chore: jwatson lesson_07 homework
1 parent 719b910 commit 76dd422

File tree

2 files changed

+52
-12
lines changed

2 files changed

+52
-12
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { error } from "console";
12
import { computeLexicographicDistance } from "./util.js";
23

34
/**
@@ -11,9 +12,8 @@ export function compareStrings(a: string, b: string): number {
1112
// The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
1213
// if it is greater, and 0 if the strings are equal.
1314
const distance = computeLexicographicDistance(a, b);
14-
15-
// TODO(you): Finish this method.
16-
15+
if (distance < 0) return -1;
16+
if (distance > 0) return 1;
1717
return 0;
1818
}
1919

@@ -24,7 +24,17 @@ export function compareStrings(a: string, b: string): number {
2424
* @return The factorial of n.
2525
*/
2626
export function computeFactorial(n: number): number {
27-
return 0;
27+
if (n < 0) {
28+
return 0;
29+
}
30+
if (n === 0 || n === 1) {
31+
return 1;
32+
}
33+
let result = 1;
34+
for (let i = 2; i <= n; i++) {
35+
result *= i;
36+
}
37+
return result;
2838
}
2939

3040
/**
@@ -34,7 +44,14 @@ export function computeFactorial(n: number): number {
3444
* @return An array containing the first `n` Fibonacci values.
3545
*/
3646
export function getFirstNFibonacciNumbers(n: number): number[] {
37-
return [];
47+
if (n <= 0) return [];
48+
if (n === 1) return [0];
49+
50+
const fibSeries: number[] = [1, 1];
51+
for (let i = 2; i < n; i++) {
52+
fibSeries.push(fibSeries[i - 1] + fibSeries[i - 2]);
53+
}
54+
return fibSeries;
3855
}
3956

4057
/**
@@ -58,12 +75,16 @@ export function binarySearch(
5875
}
5976

6077
const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.
61-
62-
// TODO(you): Finish implementing this algorithm
63-
64-
// If values[pivotIndex] is equal to value then return `pivotIndex`.
65-
// Else if values[pivotIndex] is greater than the value, then
66-
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
67-
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
78+
if (values[pivotIndex] === value) {
79+
return pivotIndex;
80+
} else if (values[pivotIndex] > value) {
81+
return binarySearch(values, start, pivotIndex - 1, value);
82+
} else {
83+
return binarySearch(values, pivotIndex + 1, end, value);
84+
}
6885
return -1;
6986
}
87+
// If values[pivotIndex] is equal to value then return `pivotIndex`.
88+
// Else if values[pivotIndex] is greater than the value, then
89+
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
90+
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.

lesson_07/conditionals/src/part_b.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66
*/
77
export function isLeapYear(year: number): boolean {
88
return false;
9+
switch (true) {
10+
case year % 4 === 0:
11+
return true;
12+
case year % 100 === 0:
13+
return false;
14+
case year % 400 === 0:
15+
return true;
16+
default:
17+
return false;
18+
}
919
}
1020

1121
/**
@@ -15,6 +25,12 @@ export function isLeapYear(year: number): boolean {
1525
* @returns
1626
*/
1727
export function isEvenOrOdd(num: number): string {
28+
if (num % 2 === 0) {
29+
return "even";
30+
}
31+
if (num % 2 !== 0) {
32+
return "odd";
33+
}
1834
return "";
1935
}
2036

@@ -25,5 +41,8 @@ export function isEvenOrOdd(num: number): string {
2541
* @returns
2642
*/
2743
export function hasVowel(word: string): boolean {
44+
if (word.match(/[aeiou]/i)) {
45+
return true;
46+
}
2847
return false;
2948
}

0 commit comments

Comments
 (0)