-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfibonacci.js
49 lines (43 loc) · 1.84 KB
/
fibonacci.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* Calculate the Fibonacci sequence
* @param {Number} numIteration
* @returns {Array} Returns an array with the length of numIteration and the Fibonacci sequence
*/
function fibs (numIteration) {
const sequence = [0, 1];
if (numIteration <= 0) { // If numIteration is less than or equal to 0, return an empty array
return 'Insert a positive number';
}
if (numIteration == 1) { // If numIteration is equal to 1, return an array with 0
return sequence.splice(0,1);
}
for (let i = numIteration; i > 2; i--) { // Loop through the numIteration
// Add the sum of the last two numbers of the Fibonacci sequence to the array
sequence.push(sequence[sequence.length - 2] + sequence[sequence.length - 1]);
}
return sequence;
}
console.log(fibs(0)); // Expected Result --> []
console.log(fibs(1)); // Expected Result --> [0]
console.log(fibs(2)); // Expected Result --> [0, 1]
console.log(fibs(3)); // Expected Result --> [0, 1, 1]
console.log(fibs(4)); // Expected Result --> [0, 1, 1, 2]
console.log(fibs(8));; // Expected Result --> [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
/**
* Calculate the Fibonacci sequence recursively
* @param {Number} numIteration
* @returns {Array} Returns an array with the length of numIteration and the Fibonacci sequence
*/
function fibsRec (numIteration, sequence = [0,1]) {
if (numIteration <= sequence.length) {
return sequence.splice(0, numIteration);
}
// Calculate the next number of the Fibonacci sequence
const nextNumber = sequence.at(-2) + sequence.at(-1);
sequence.push(nextNumber); // Add the next number to the array
return fibsRec(numIteration, sequence) // Recursive call
}
console.log(fibsRec(0)); // Expected Result --> []
console.log(fibsRec(1)); // Expected Result --> [0]
console.log(fibsRec(5)); // Expected Result --> [0]
console.log(fibsRec(10)); // Expected Result --> [0]