A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Fibonacci in Javascript page! Here, you'll find the source code for this program as well as a description of how the program works.
function fibonacci(num) {
let n = Number(num);
let first = 0;
let second = 1;
let result = 0;
for (let i = 1; i <= n; i++) {
result = first + second;
first = second;
second = result;
console.log(i + ": " + first);
}
}
num = process.argv[2];
if (num && !isNaN(num)) {
fibonacci(num);
} else {
console.log("Usage: please input the count of fibonacci numbers to output")
}
Fibonacci in Javascript was written by:
This article was written by:
If you see anything you'd like to change or update, please consider contributing.
Let's look at the code in detail:
Here we have a function called fibonacci
that takes in a numeric value as an argument that corresponds to the amount Fibonacci numbers we want to print in succession.
What fibonacci
" does is that it starts printing from 1 then each time it just prints the accumulation of the last number it printed (stored in variable named second
) and the 2nd last number it printed (stored in variable named first
).
Then we have a variable named num
which can have a numeric value of 10
since we want to print the first 10 numbers in the Fibonacci sequence. I can also have the value process.argv[2]
so we can run the command node fibonacci.js 10
to execute the file with NodeJS and print the first 10 numbers in the Fibonacci sequence.
Then we have a function that verifies that num
has a positive, numeric value so we can run the function named fibonacci
, else it just returns an instruction/warning.
num
a value of 10 instead of process.argv[2]
.For example:
If you copy/paste this code in a file named fibonacci.js
then use copy/paste the following tag in your HTML file:
<script src="fibonacci.js"></script>
fibonacci.js
is in the same folder/directory as your HTML file.num
has a value of `process.argv[2]".fibonacci.js
.node fibonacci.js 10
to execute the file with NodeJS to print the first 10 numbers in the Fibonacci sequence.