Javascript Algorithms | Fibonacci series
Fibonacci series is often referred to as the golden ratio in the mathematical world, In our case, however, we print a series of characters also referred to as a pattern like so 0,1,1,2,3,5.. etc the series here indicates that every number in the series is the sum of previous two numbers for example
- 0 + 1 will be 1
- 1 + 1 will be 2
- 1 + 2 will be 3
- 2 + 3 will be 5 and so on
for the solution, we define a function and expect an argument of count which indicates the number of iteration for the Fibonacci series, inside the function, we declare a variable named series of the type array and add the default values of 0 and 1 this needed.
Then we define a for loop and push the sum of the current index item of the series array and then the next item in the series array, once the loop is complete we just print the results to the console.
PS: Notice the for loop (i<count) indicates that index (i) will always be less than the count argument.
JavaScript solution for Fibonacci series
function fibonacci(count){
let series =[0,1];
for(let i=0;i<count;i++){
series.push(series[i]+series[i+1]);
}
console.log(series);
}
fibonacci(10);