Skip to content

Commit c356b2f

Browse files
committed
refactor: nested if statement in for loop
1 parent 880fe86 commit c356b2f

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,22 @@ export function computeFactorial(n: number): number {
5050
* @return An array containing the first `n` Fibonacci values.
5151
*/
5252
export function getFirstNFibonacciNumbers(n: number): number[] {
53-
const arr: number[] = [1, 1];
54-
let prev = 1;
53+
const arr: number[] = [];
5554
let curr = 1;
55+
let prev = 1;
5656
let next;
5757

58-
if (n < 1) {
59-
return [];
60-
} else if (n === 1) {
61-
return [1];
58+
for (let i = 0; i < n; i++) {
59+
if (i < 2) {
60+
arr[i] = 1;
61+
} else {
62+
next = curr + prev;
63+
arr[i] = next;
64+
prev = curr;
65+
curr = next;
66+
}
6267
}
6368

64-
for (let i = 2; i < n; i++) {
65-
next = prev + curr;
66-
arr[i] = next;
67-
prev = curr;
68-
curr = next;
69-
}
7069
return arr;
7170
}
7271

0 commit comments

Comments
 (0)