Last active
October 5, 2022 08:44
-
-
Save MVAodhan/faee5425589624b497d3ab621c640361 to your computer and use it in GitHub Desktop.
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
/** | |
* Given two integers, generate a “fibonacci-like” sequence of n digits | |
* (where the next number in the pattern is the sum of the previous two numbers). | |
* Extra credit: Given a sequence, determine if the sequence is “fibonacci-like”. | |
*/ | |
let n = 5 | |
const fibLike = (num1, num2, n) => { | |
let nums = [num1, num2]; | |
for (i = 0; nums.length < n; i++) { | |
let nextNum = nums.at(-1); | |
let prevNum = nums.at(-2); | |
let nextSeq = nextNum + prevNum; | |
nums = [...nums, nextSeq]; | |
} | |
return nums; | |
}; | |
// > fibLike(10, 20, n) | |
// > [10, 20, 30, 50, 80] | |
// > fibLike(3, 7, n) | |
// > [3, 7, 10, 17, 27] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment