Skip to content

Commit cfacb28

Browse files
committed
chore: adds functions for lesson7 and part_d.ts
1 parent 139ff82 commit cfacb28

File tree

2 files changed

+24
-36
lines changed

2 files changed

+24
-36
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@ import { computeLexicographicDistance } from "./util.js";
77
* @param b The second `string` to compare.
88
* @return -1 if a is less than b, 1 if a is greater than b, and 0 otherwise.
99
*/
10-
export function compareStrings(a: string, b: string): number {
11-
const distance = computeLexicographicDistance(a, b);
12-
13-
if (distance < 0) {
10+
function compareStrings(a: string, b: string): number {
11+
if (a < b) {
1412
return -1;
15-
} else if (distance > 0) {
13+
} else if (a > b) {
1614
return 1;
1715
} else {
1816
return 0;
1917
}
18+
2019
}
2120

2221
/**
@@ -25,7 +24,7 @@ export function compareStrings(a: string, b: string): number {
2524
* @param n The value for which to compute the factorial.
2625
* @return The factorial of n.
2726
*/
28-
export function computeFactorial(n: number): number {
27+
function computeFactorial(n: number): number {
2928
if (n === 0) {
3029
return 1;
3130
}
@@ -44,21 +43,17 @@ export function computeFactorial(n: number): number {
4443
* @param n The first `n` of Fibonacci values to compute.
4544
* @return An array containing the first `n` Fibonacci values.
4645
*/
47-
export function getFirstNFibonacciNumbers(n: number): number[] {
48-
if (n <= 0) return [];
49-
50-
const fib: number[] = [1];
51-
52-
if (n === 1) return fib;
46+
function getFirstNFibonacciNumbers(n: number): number[] {
47+
let fib: number[] = [];
5348

54-
fib.push(1); // Add the second 1
55-
56-
for (let i = 2; i < n; i++) {
57-
const next = fib[i - 1] + fib[i - 2];
58-
fib.push(next);
49+
for (let i = 0; i < n; i++) {
50+
if (i === 0 || i ===1) {
51+
fib.push(1);
52+
} else {
53+
fib.push(fib[i - 1] + fib[i -2]);
54+
}
5955
}
60-
61-
return fib;
56+
return fib;
6257
}
6358

6459
/**
@@ -70,23 +65,16 @@ export function getFirstNFibonacciNumbers(n: number): number[] {
7065
* @param value The value to look for.
7166
* @return The index of the value if found in the array and -1 otherwise.
7267
*/
73-
export function binarySearch(
68+
function findValue(
7469
values: number[],
7570
start: number,
7671
end: number,
77-
value: number,
72+
value: number
7873
): number {
79-
if (end < start) {
80-
return -1; // Base case: value not found
81-
}
82-
83-
const pivotIndex = Math.floor((start + end) / 2);
84-
85-
if (values[pivotIndex] === value) {
86-
return pivotIndex;
87-
} else if (values[pivotIndex] > value) {
88-
return binarySearch(values, start, pivotIndex - 1, value);
89-
} else {
90-
return binarySearch(values, pivotIndex + 1, end, value);
74+
for (let i+ start; i <= end; i++) {
75+
if (values[i] === value) {
76+
return i;
77+
}
9178
}
79+
return -1;
9280
}

lesson_07/conditionals/src/part_d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @param max
77
* @returns
88
*/
9-
export function isWithinRange(num: number, min: number, max: number): boolean {
9+
function isWithinRange(num: number, min: number, max: number): boolean {
1010
return num >= min && num <= max;
1111
}
1212

@@ -21,7 +21,7 @@ console.log(isWithinRange(2, 5, 10));
2121
* @param c
2222
* @returns
2323
*/
24-
export function isValidTriangle(a: number, b: number, c: number): boolean {
24+
function isValidTriangle(a: number, b: number, c: number): boolean {
2525
return a + b > c && a + c > b && b + c > a;
2626
}
2727

@@ -32,7 +32,7 @@ export function isValidTriangle(a: number, b: number, c: number): boolean {
3232
* @param month
3333
* @returns
3434
*/
35-
export function getSeason(month: number): string {
35+
function getSeason(month: number): string {
3636
if (month >= 3 && month <= 5) {
3737
return "Spring";
3838
} else if (month >= 6 && month <= 8) {

0 commit comments

Comments
 (0)