I have a working solution for this Kata (find nth value of Fibonacci Sequence), however I keep getting a timeout error. Can anyone offer advice on how to refactor this to run more efficiently? Thanks in advance!
Here is the link with description - https://www.codewars.com/kata/simple-fun-number-395-fibonacci-digit-sequence/train/javascript
You are given three non negative integers a, b and n, and making an infinite sequence just like fibonacci sequence, use the following rules:
step1: use ab as the initial sequence. step2: calculate the sum of the last two digits of the sequence, and append it to the end of sequence. repeat step2 Your task is to complete function find. Return nth digit(0-based) of the sequence.
function find(a,b,n){
let start = ("" + a + b);
let next = a + b;
let seq = start + next;
while (seq.length <= n) {
seq += (parseInt(seq[seq.length-2]) + parseInt(seq[seq.length-1]));
}
return parseInt(seq[n]);
}
console.log(find(7,8,9))
// should return 5