fibonacci sums javascript
// 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));// Implement a method that finds the sum of the first n
// fibonacci numbers recursively. Assume n > 0
function fibsSum(n) {
if ( n === 1 ) {
return 1;
}
if (n === 2 ) {
return 2;
}
let sum = fibsSum(n-1) + n;
return sum;
}
Also in JavaScript:
- js obj
- javascript loop nested object
- jquery add element array
- how to target two id using jquery
- navigation in react native
- regular expression not to allow space in javascript
- jquery get all classes of a div
- angularjs filter array of objects based on name prop
- fibonacci javascript
- .filter js
- Create a 'uniqueWords' function that accepts a string 'str' and returns an array of all the unique words in that string.
- javascript capitalize words
- lodash npm
- example of while loop in javascript with array length
- open popup in angular
- javascript format currency
- js canvas draw polygon
- how to learn javascript
- how to send data using ajax in php
- javascript removing item from array
- search array for property js
- javascript event bind dynamic element
- remove all options from select jquery
- defining props in react