I have written a code below for printing the first n fibonacci numbers,
the code is as follows
var fibo = function (n){
var num = 1, prevNum = 1, currentNum = 1, printNum = [];
do{
if(num == 1){
prevNum = num;
currentNum = num;
printNum.push(num);
num++;
}else if(num == 2){
currentNum = num;
printNum.push(num);
num++;
}else{
num = currentNum + prevNum;
printNum.push(num);
prevNum = currentNum;
currentNum = num;
}
n--;
}while(n >= 1);
console.log(printNum.toString());
}
fibo(6);
fibo(10);
fibo(20);
The output of the above program is as follows,
PS D:\> node .\fibonacci.js
1,2,3,5,8,13
1,2,3,5,8,13,21,34,55,89
1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946
PS D:\>
Is there a better way in implementing it ?
please review my code
3 Answers 3
For readability, I prefer the (pythonesque?!) destructuring assignment
(and array over, say, BigUint64Array[n]
).
I do not find value added by the name suffix Num
.
When not intending to change the meaning of fibo
at runtime, I'd rather declare it a function.
Somehow, special cases (1, 2) crept into your implementation.
Separation of concerns would would split out logging - I kept fibo()
s interface below:
/** Return an array with the first n elements of the Fibonacci sequence.
* If the array is non-empty, log it to the console. */
function fibo(n) {
if (n <= 0)
return []
var a = 1, b = 1,
sequence = [b]
while (0 < --n) {
[a, b] = [b, a + b]
sequence.push(b)
}
console.log(sequence.toString())
return sequence
}
console.log(fibo(0))
fibo(6); fibo(10); fibo(20);
For JSDoc, add tags as you like
* @argument n the number of elements
* @return an array with the first n elements of the Fibonacci sequence.
JavaScript number primitives and Number objects use IEEE 754 Floating-Point Arithmetic. The Number.MAX_SAFE_INTEGER constant represents the maximum safe integer in JavaScript (2^53 - 1). Use JavaScript BigInt to avoid floating-point approximations for large integer values. Use JavaScript BigInt to avoid printing large integer values in exponential notation.
Keep things simple. Why do you complicate the computation with if () else if () else?
A sequence of Fibonacci numbers has value independent of its String output format. Don't conflate the computation and the toString output of the sequence by doing everything in a single function.
The On-Line Encyclopedia of Integer Sequences (OEIS) definition of Fibonacci numbers:
OEIS: A000045
Fibonacci numbers: F(n) = F(n-1) + F(n-2) with F(0) = 0 and F(1) = 1.
https://oeis.org/A000045
In JavaScript, a sequence of n Fibonacci numbers:
function fibonacci(n) {
let fibs = [];
let [a, b] = [BigInt(0), BigInt(1)];
for (let i = 0; i < n; i++) {
fibs.push(a);
[a, b] = [b, a + b];
}
return fibs;
}
console.log(fibonacci(150).toString());
0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,
. . .
,10284720757613717413913,16641027750620563662096,26925748508234281076009
An older Fibonacci number definition starts the sequence with 1, 1.
let [a, b] = [BigInt(1), BigInt(1)];
Fibonacci started the sequence with with 1, 2.
let [a, b] = [BigInt(1), BigInt(2)];
n--; }while(n >= 1);
You don't need to consume the input argument. You already have an up-to-date value.
} while (n > printNum.length);
If you start with
const printNum = [1, 2];
You can close with
return printNum.slice(0, n);
Then you don't need any special cases. Just
const fibs = [1, 2];
while (fibs.length < n) {
fibs.push(fibs[fibs.length - 1] + fibs[fibs.length - 2]);
}
return fibs.slice(0, n);
Note that this also gets rid of the redundant variables and just works on the array.
Explore related questions
See similar questions with these tags.
printNum
and only print it when the last element is computed? \$\endgroup\$comparative-review
- I find only one solution presented.) \$\endgroup\$