Created
January 2, 2018 13:10
-
-
Save lhuria94/922eeab75dbeef7d28d0d84eb645b900 to your computer and use it in GitHub Desktop.
Build a Xbonacci function that takes a signature of X elements - and remember each next element is the sum of the last X elements - and returns the first n elements of the so seeded sequence.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This solves the problem with last and previous digit addition. | |
function Xbonacci(signature, n){ | |
var totalCallsToMake = n - signature.length; | |
var tempArr = signature.slice(0); | |
const reducer = (accumulator, currentValue) => accumulator + currentValue; | |
// Looping through. | |
for (var i=1; i<=totalCallsToMake; i++) { | |
var lastTwoEl = tempArr.slice(-2); | |
tempArr.push(lastTwoEl.reduce(reducer)); | |
} | |
return tempArr; | |
} | |
Xbonacci([0,0,0,0,1],10); | |
// This solves the problem with last n digits. | |
function Xbonacci(signature, n){ | |
var totalCallsToMake = n - signature.length; | |
var tempArr = signature.slice(0); | |
const reducer = (accumulator, currentValue) => accumulator + currentValue; | |
// Looping through. | |
for (var i=1; i<=totalCallsToMake; i++) { | |
tempArr.push(tempArr.reduce(reducer)); | |
} | |
return tempArr; | |
} | |
Xbonacci([0,1],10); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment