Skip to content

Commit 7ece360

Browse files
committed
Feat Not completed lesson07. Have a meeting scheduled with JE to go over syntax
1 parent a4c4623 commit 7ece360

File tree

3 files changed

+68
-27
lines changed

3 files changed

+68
-27
lines changed

lesson_07/conditionals/src/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function canVote(age:number); boolean{
2+
if (age >= 18) {
3+
return true;
4+
}
5+
else {
6+
return false;
7+
}
8+
}
9+
10+
console.log(canVote(18));

lesson_07/conditionals/src/lesson7.ts

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ import { computeLexicographicDistance } from "./util.js";
88
* @return -1 if a is less than b, 1 if a is greater than b, and 0 otherwise.
99
*/
1010
export function compareStrings(a: string, b: string): number {
11-
// The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
12-
// if it is greater, and 0 if the strings are equal.
1311
const distance = computeLexicographicDistance(a, b);
14-
15-
// TODO(you): Finish this method.
12+
13+
if (a < b) return -1;
14+
if (a > b) return 1;
1615

1716
return 0;
1817
}
@@ -24,19 +23,36 @@ export function compareStrings(a: string, b: string): number {
2423
* @return The factorial of n.
2524
*/
2625
export function computeFactorial(n: number): number {
27-
return 0;
26+
27+
if (n < 0) {
28+
console.error("Factorial is not defined for negative numbers.");
29+
return -1;
30+
}
31+
if (n === 0 || n === 1) return 1;
32+
return n * computeFactorial(n - 1);
2833
}
2934

35+
console.log(computeFactorial(-2));
36+
37+
38+
3039
/**
3140
* Returns an array of the first `n` Fibonacci numbers starting from 1.
3241
*
3342
* @param n The first `n` of Fibonacci values to compute.
3443
* @return An array containing the first `n` Fibonacci values.
3544
*/
3645
export function getFirstNFibonacciNumbers(n: number): number[] {
37-
return [];
46+
if (n <= 0) return [];
47+
if (n === 1) return [1];
48+
49+
const fib: number[] = [1, 1];
50+
for (let i = 2; i < n; i++) {
51+
fib.push(fib[i - 1] + fib[i - 2]);
52+
}
53+
return fib;
3854
}
39-
55+
4056
/**
4157
* Finds a value in an array of values.
4258
*
@@ -46,24 +62,23 @@ export function getFirstNFibonacciNumbers(n: number): number[] {
4662
* @param value The value to look for.
4763
* @return The index of the value if found in the array and -1 otherwise.
4864
*/
49-
export function binarySearch(
50-
values: number[],
51-
start: number,
52-
end: number,
53-
value: number,
54-
): number {
55-
if (end < start) {
56-
// The range is not valid so just return -1.
57-
return -1;
58-
}
65+
export function binarySearch(values: number[], value: number): number {
66+
let start = 0;
67+
let end = values.length - 1;
5968

60-
const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.
69+
while (start <= end) {
70+
let mid = Math.floor((start + end) / 2);
6171

62-
// TODO(you): Finish implementing this algorithm
72+
if (values[mid] === value) {
73+
return mid;
74+
} else if (values[mid] > value) {
75+
end = mid - 1;
76+
} else {
77+
start = mid + 1;
78+
}
79+
}
6380

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.
68-
return -1;
81+
return -1;
6982
}
83+
84+

lesson_07/conditionals/src/part_a.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,28 @@
55
* @return True if the age corresponds to a voting age and false otherwise.
66
*/
77
export function canVote(age: number): boolean {
8-
return false;
8+
if (age >= 18) {
9+
return true;
10+
}
11+
else {
12+
return false;
13+
}
914
}
1015

16+
17+
1118
/**
1219
* Adds all of the provided values and returns the sum.
1320
*
1421
* @param values The values to sum.
1522
* @return The sum of all the values.
1623
*/
1724
export function addNumbers(values: number[]): number {
18-
return 0;
25+
let sum= 0;
26+
for (let i = 0; i < values.length; i++) {
27+
sum += values[i];
28+
}
29+
return sum;
1930
}
2031

2132
/**
@@ -25,5 +36,10 @@ export function addNumbers(values: number[]): number {
2536
* @return The factorial of n.
2637
*/
2738
export function computeFactorial(n: number): number {
28-
return 0;
39+
40+
let result = 1;
41+
for (let i = 1; i <= n; i++) {
42+
result *= i;
43+
}
44+
return result;
2945
}

0 commit comments

Comments
 (0)