javascript fibonacci example
function fibonacci(n) {
return n < 1 ? 0
: n <= 2 ? 1
: fibonacci(n - 1) + fibonacci(n - 2);
}
console.log(fibonacci(4));
// 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;
}// A sequência começa com 0 e 1, depois,
// os números subsequentes são compostos
// pela soma dos dois números anteriores
// da sequência. Daí:
// (x - 2) + (x - 1) = próximo número da sequência
function fibonacci(n) {
const list = [0, 1];
for (let x = 2; x < n + 1; x += 1) {
list.push(list[x - 2] + list[x - 1]);
}
return list[n];
}
console.log(fibonacci(4));function fibonacci(num){
var num1=0;
var num2=1;
var sum;
var i=0;
for (i = 0; i < num; i++){
sum=num1+num2;
num1=num2;
num2=sum;
}
return num2;
}
Also in JavaScript:
- add one file to another in ejs
- clearing cookie in js
- how to call function on every keypress in jquery
- define value in js
- javascript check image src
- Code is valid JSON equivalent of the key/value pair shown that also preserves the original value: UPC: 043875
- créer composant react
- e editable select no button
- google gapi auth2 get current token
- nodejs connection to localhost refused mysql errno -61
- how to store variable in javascript
- concat array javascript
- iterate through object array javascript
- not operator js
- range javascript
- defining props in react
- kotlin jsonobject from string
- json stringify close circle
- array reduce and count based on proeperty js
- recuperer une image dans la base de données avec angular
- Object as a Function
- javascript add new array element to start of array
- create moment duration
- javascript load image programmatically