Skip to content

Instantly share code, notes, and snippets.

@MVAodhan
Last active October 5, 2022 08:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MVAodhan/faee5425589624b497d3ab621c640361 to your computer and use it in GitHub Desktop.
Save MVAodhan/faee5425589624b497d3ab621c640361 to your computer and use it in GitHub Desktop.
/**
* 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