how to generate a fibonacci sequence in javascript
// declare the array starting with the first 2 values of the fibonacci sequence
let fibonacci = [0,1];
function listFibonacci(num) {
// starting at array index 1, and push current index + previous index to the array
for (let i = 1; i < num; i++) {
fibonacci.push(fibonacci[i] + fibonacci[i - 1]);
}
console.log(fibonacci);
}
listFibonacci(10);
// number fibonnaci to array format
function fibonacci(nums) {
let fib = [0, 1];
let data = [];
for(let i = 2; i <= nums; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
data.push(fib[i]);
}
return data;
}var i;
var fib = []; // Initialize array!
fib[0] = 0;
fib[1] = 1;
for (i = 2; i <= 10; i++) {
// Next fibonacci number = previous + one before previous
// Translated to JavaScript:
fib[i] = fib[i - 2] + fib[i - 1];
console.log(fib[i]);
}
Also in JavaScript:
- Parallax.js wrapper for react
- jquery get value radio by name
- toast in react native
- adding bootstrap to angular
- how to sho the active navigation ling using javascript
- récupérer la position d'un élément javascript
- rails call multiple variable format json
- counting duplicate values javascript
- disable a button react
- Javascript compare two objects
- ternary operator in javascript
- Amazon VPC supporting 5 different IP address ranges and i wanted to know how wide those rangers are
- how to append data to a field in mongoose model
- open json file r
- filter even numbers javascript
- javscript append item from array
- state class component react
- function that search a biggest value in array javascript
- how to pass custom regex in jquery validation
- check if is array or object javascript
- Code is valid JSON equivalent of the key/value pair shown that also preserves the original value: UPC: 043875
- js replace text link with anchor tags
- js toFixed
- angular access current scope from console