0

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 ?

4
  • You're testing n > max_number. Did you mean n >= max_number to exclude max_number from the logged list? Commented Jun 10, 2018 at 9:29
  • yes, it is.it needs = Commented Jun 10, 2018 at 9:30
  • 1
    I get the impression you intend for max_number to 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 to max_number. That is, fibonacciseries(20) doesn't log 20 numbers, it logs all Fibonacci numbers that are less than or equal to 20. Commented Jun 10, 2018 at 10:16
  • @nnnnnn yes, you are right.but how should I fix it? Commented Jun 10, 2018 at 10:22

2 Answers 2

1

check the n>=max_number not n>max_number. Try the below updated code

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)

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for your answer.When I run the codes fibonacciseries(5),only 1 1 2 3 is shown.What is wrong??
0

You seem to want max_number to 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 to max_number. That is, fibonacciseries(20) doesn't log 20 numbers, it logs all Fibonacci numbers that are less than or equal to 20. It's just a coincidence that fibonacciseries(4) & fibonacciseries(5) happen to produce the same result either way.

To fix this, don't compare max_number to n, just decrement max_number on each call and stop when it gets to zero. To make this a bit clearer I suggest renaming that argument:

function fibonacciseries(count, p, n) {
  if(p === undefined) { p = 0;}
  if(n === undefined) { n = 1;}
  if(count <= 0) { return 0; }
  console.log(n);
  return fibonacciseries(count - 1, n, n + p)
}

console.log("Count of 3:");
fibonacciseries(3);
console.log("Count of 5:");
fibonacciseries(5);
console.log("Count of 7:");
fibonacciseries(7);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.