Skip to content

Commit e3b64ad

Browse files
feat: adds Karens lesson_07.ts
1 parent 089c75e commit e3b64ad

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

lesson_07/conditionals/.env.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
HW_VERSION=your homework version here
1+
HW_VERSION=D

lesson_07/conditionals/src/lesson7.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,13 @@ export function compareStrings(a: string, b: string): number {
1111
// The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
1212
// if it is greater, and 0 if the strings are equal.
1313
const distance = computeLexicographicDistance(a, b);
14-
if (distance < 0){
14+
if (distance < 0) {
1515
return -1;
16-
}
17-
if (distance > 0) {
18-
return 1;
19-
}
20-
if (distance === 0) {
16+
} else if (distance > 0) {
2117
return 1;
18+
} else {
19+
return 0;
2220
}
23-
return 0;
2421
}
2522

2623
/**
@@ -40,7 +37,20 @@ export function computeFactorial(n: number): number {
4037
* @return An array containing the first `n` Fibonacci values.
4138
*/
4239
export function getFirstNFibonacciNumbers(n: number): number[] {
43-
return [];
40+
/* fibionacci formula - f(n) = f(n-1) + f(n-2)... */
41+
if (n === 0) {
42+
return [];
43+
}
44+
if (n === 1) {
45+
return [1];
46+
}
47+
// create array
48+
const number: number[] = [1, 1];
49+
for (let i = 2; i < n; i++) {
50+
//iterate thorugh items in array
51+
number.push(number[i - 1] + number[i - 2]);
52+
}
53+
return number;
4454
}
4555

4656
/**
@@ -71,5 +81,11 @@ export function binarySearch(
7181
// Else if values[pivotIndex] is greater than the value, then
7282
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
7383
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
74-
return -1;
84+
if (values[pivotIndex] === value) {
85+
return pivotIndex;
86+
} else if (values[pivotIndex] > value) {
87+
return binarySearch(values, start, pivotIndex - 1, value);
88+
} else {
89+
return binarySearch(values, pivotIndex + 1, end, value);
90+
}
7591
}

lesson_07/conditionals/src/part_d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @returns
88
*/
99
export function isWithinRange(num: number, min: number, max: number): boolean {
10-
return false;
10+
1111
}
1212

1313
/**

0 commit comments

Comments
 (0)