I'm having an issue with accuracy of my code.
I made a function to calculate the Fibonacci series:
function fibonacciseries(max_number, p, n) {
if(p === undefined) { p = 0;}
if(n === undefined) { n = 1;}
if(n > max_number) { return 0; }
console.log(n);
return fibonacciseries(max_number, n, n + p)
}
fibonacciseries(3)
but when I run this code, console.log(n); shows:
1
1
2
3
I think result should be 1 1 2, so I really cannot understand why such a thing happens. Running fibonacciseries(4) & fibonacciseries(5) are OK, so what is wrong in the case of fibonacciseries(3) ? How should I fix this ?
n > max_number. Did you meann >= max_numberto excludemax_numberfrom the logged list?max_numberto be a count of how many numbers the function should log, but what it is actually doing is logging all Fibonacci numbers less than or equal tomax_number. That is,fibonacciseries(20)doesn't log 20 numbers, it logs all Fibonacci numbers that are less than or equal to 20.