Skip to content

Commit ffbdf89

Browse files
author
Dadenaike251
committed
hw incomplete
1 parent b9be8c7 commit ffbdf89

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ 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-
14+
if (distance < 0) {
15+
return -1;
16+
} else if (distance > 0) {
17+
return 1;
18+
} else {
19+
return 0;
20+
}
1521
// TODO(you): Finish this method.
16-
17-
return 0;
1822
}
1923

2024
/**
@@ -24,7 +28,16 @@ export function compareStrings(a: string, b: string): number {
2428
* @return The factorial of n.
2529
*/
2630
export function computeFactorial(n: number): number {
27-
return 0;
31+
if (n < 0) {
32+
return 0;
33+
}
34+
let numbers = 1;
35+
36+
for (let i = 1; i < n; n--) {
37+
numbers = numbers * n;
38+
}
39+
40+
return numbers;
2841
}
2942

3043
/**
@@ -34,6 +47,16 @@ export function computeFactorial(n: number): number {
3447
* @return An array containing the first `n` Fibonacci values.
3548
*/
3649
export function getFirstNFibonacciNumbers(n: number): number[] {
50+
if (n === 0) {
51+
return [];
52+
}
53+
if (n === 1) {
54+
return [1];
55+
}
56+
const number: number[] = [1, 1];
57+
for (let i = 2; i < n; i++) {
58+
number.push(number[i - 1] + number[i - 2]);
59+
}
3760
return [];
3861
}
3962

@@ -65,5 +88,12 @@ export function binarySearch(
6588
// Else if values[pivotIndex] is greater than the value, then
6689
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
6790
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
91+
if (values[pivotIndex] === value) {
92+
return pivotIndex;
93+
} else if (values[pivotIndex] > value) {
94+
return binarySearch(values, start, pivotIndex - 1, value);
95+
} else {
96+
return binarySearch(values, start, pivotIndex + 1, end);
97+
}
6898
return -1;
6999
}

lesson_07/conditionals/src/part_a.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
* @return True if the age corresponds to a voting age and false otherwise.
66
*/
77
export function canVote(age: number): boolean {
8+
if (age >= 18) {
9+
return true;
10+
}
811
return false;
912
}
1013

@@ -15,9 +18,16 @@ export function canVote(age: number): boolean {
1518
* @return The sum of all the values.
1619
*/
1720
export function addNumbers(values: number[]): number {
18-
return 0;
21+
if (values.length === 0) {
22+
return 0;
23+
} else {
24+
let sum = 0;
25+
for (let i = 0; i < values.length; i++) {
26+
sum += values[i];
27+
}
28+
return sum;
29+
}
1930
}
20-
2131
/**
2232
* Computes the factorial of the given value of `n`.
2333
*

0 commit comments

Comments
 (0)