Would really appreciate an explanation on the given solution below. How does JavaScript 'store' the s variable as an array as I would think it continually would get overwritten as the s.push(s[s.length - 1] + s[s.length - 2]); line doesn't get hit until after the recursion?
From my understanding JavaScript reads left to write then up to down, so it will never hit the push line until after it has gone through all cycles of s?
How its storing it an array?
function fibonacci(n) {
if (n === 1) {
return [0, 1];
} else {
var s = fibonacci(n - 1);
s.push(s[s.length - 1] + s[s.length - 2]);
return s;
}
}
console.log(fibonacci(8));
[0, 1]so in simple termsis an array