Skip to content

Commit 4812604

Browse files
author
Dadenaike251
committed
fix: Completed homework & Passed all Tests
1 parent ffbdf89 commit 4812604

File tree

3 files changed

+81
-29
lines changed

3 files changed

+81
-29
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=A

lesson_07/conditionals/src/lesson7.ts

Lines changed: 64 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,28 @@ export function compareStrings(a: string, b: string): number {
2828
* @return The factorial of n.
2929
*/
3030
export function computeFactorial(n: number): number {
31-
if (n < 0) {
31+
if (n === 0) {
32+
return 1;
33+
}
34+
else if (n === 1) {
35+
return 1;
36+
}
37+
else if (n < 0) {
3238
return 0;
3339
}
34-
let numbers = 1;
35-
36-
for (let i = 1; i < n; n--) {
37-
numbers = numbers * n;
40+
else {
41+
return n * computeFactorial(n-1);
3842
}
43+
// if (n < 0) {
44+
// return 0;
45+
// }
46+
// let numbers = 1;
3947

40-
return numbers;
48+
// for (let i = 1; i < n; n--) {
49+
// numbers = numbers * n;
50+
// }
51+
52+
// return numbers;
4153
}
4254

4355
/**
@@ -47,17 +59,39 @@ export function computeFactorial(n: number): number {
4759
* @return An array containing the first `n` Fibonacci values.
4860
*/
4961
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]);
62+
63+
for (let i = 0; i < n; i++) {
64+
if (n === 0)
65+
return[];
66+
else if (n === 1) {
67+
return [1];
68+
}
69+
else if (n === 2) {
70+
return[1,1];
71+
}
72+
else {
73+
const fib = getFirstNFibonacciNumbers(n-1);
74+
fib.push(fib[fib.length - 1] + fib [fib.length -2]);
75+
return fib;
76+
}
5977
}
60-
return [];
78+
return[];
79+
80+
// if (n === 0) {
81+
// return [];
82+
// }
83+
// else if(n === 1) {
84+
// return [1];
85+
// } else if (n === 2) {
86+
// return [2]
87+
// } else {
88+
// const number: number[] = [1, 1];
89+
// for (let i = 3; i < n; i++) {
90+
// number.push(number[i - 1] + number[i - 2]);;
91+
92+
// }
93+
// return number;
94+
// }
6195
}
6296

6397
/**
@@ -69,6 +103,7 @@ export function getFirstNFibonacciNumbers(n: number): number[] {
69103
* @param value The value to look for.
70104
* @return The index of the value if found in the array and -1 otherwise.
71105
*/
106+
72107
export function binarySearch(
73108
values: number[],
74109
start: number,
@@ -80,20 +115,24 @@ export function binarySearch(
80115
return -1;
81116
}
82117

83-
const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.
118+
const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array. //this is a variable "pivotIndex" that finds the middle values of our array
119+
if (values[pivotIndex] === value) {
120+
//this function checks if middle value index of our array is equal to the value in our array
121+
return pivotIndex; // returns said value if it is
122+
} else if (values[pivotIndex] > value) {
123+
// checks if middle value is greater than our targett value, if it is than our target is in left half
124+
return binarySearch(values, start, pivotIndex - 1, value); //calls the binarySearch function but range is narrowed down to left side from start to pivotIndex -1
125+
} else {
126+
//if middle value is less than the target value then the target must be in the right half
127+
return binarySearch(values, pivotIndex + 1, end, value); // calls function binarySearch again and limit range to right side from pivotIndex +1 to end
128+
}
84129

85130
// TODO(you): Finish implementing this algorithm
86131

87132
// If values[pivotIndex] is equal to value then return `pivotIndex`.
88133
// Else if values[pivotIndex] is greater than the value, then
89134
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
90135
// 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-
}
98-
return -1;
136+
137+
99138
}

lesson_07/conditionals/src/part_a.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,17 @@ export function canVote(age: number): boolean {
1818
* @return The sum of all the values.
1919
*/
2020
export function addNumbers(values: number[]): number {
21+
// const array1 = ["a", "b", "c"];
22+
23+
// for (const element of array1) {
24+
// console.log(element);
25+
// }
2126
if (values.length === 0) {
2227
return 0;
2328
} else {
2429
let sum = 0;
25-
for (let i = 0; i < values.length; i++) {
26-
sum += values[i];
30+
for (const value of values) {
31+
sum += value;
2732
}
2833
return sum;
2934
}
@@ -35,5 +40,13 @@ export function addNumbers(values: number[]): number {
3540
* @return The factorial of n.
3641
*/
3742
export function computeFactorial(n: number): number {
38-
return 0;
43+
if (n < 0) {
44+
return 0;
45+
}
46+
let numbers = 1;
47+
48+
for (let i = 1; i < n; n--) {
49+
numbers = numbers * n;
50+
}
51+
return numbers;
3952
}

0 commit comments

Comments
 (0)