1
\$\begingroup\$

This is my third day of learning Javascript through an online class. I learned about arrays and while/for loops today, and to practice all of that stuff the challenge I'm working on is to make a function that produces \$(n)\$ terms of the Fibonacci sequence in an array.

So I'm hoping for some feedback on this code! I think it works but I would like to know how I could make it more efficient or better in any way.

function fibonacciGenerator (n) {
    var output=[];
    if (n===1) {
      output=[0];
    } else if (n===2) {
      output=[0,1];
    } else if (n>2) {
      output=[0,1];
      for (var i=3;i<=n;i++) {
        output.push(output[i-3]+output[i-2]);
      }
    }
    return output;
}
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

For a beginner this code looks okay. Good job using strict equality comparisons - i.e. ===. The indentation is mostly consistent, although the first indentation level is 4 spaces while all subsequent levels appear to be 2 spaces.

The biggest thing I would change is spacing. For the sake of readability, it helps to add spacing around operators - especially binary operators like ===, >, =, etc. You could utilize a linter like ESLint, JSHint, etc. ESLint has rules like space-infix-ops.

While formatting preferences are very personal, a number of style guides require spaces around operators, such as:

var sum = 1 + 2; 

The proponents of these extra spaces believe it make the code easier to read and can more easily highlight potential errors, such as:

var sum = i+++2; 

While this is valid JavaScript syntax, it is hard to determine what the author intended.

1

I tried passing 0 to the function and it returned [0, 1], which is the same output if I pass 2. Theoretically it should return an empty array when 0 is passed.

The first and second else cases could be simplified to just an else, since the loop won't run unless i is less than n and for the case of 2 it would never be the case that 3 is less than 2. So it can be simplified like this:

function fibonacciGenerator (n) {
    var output = [];
    if (n === 1) {
      output = [0];
    } else {
      output = [0, 1];
      for (var i=3; i <= n; i++) {
        output.push(output[i-3] + output[i-2]);
      }
    }
    return output;
}

In more advanced JavaScript the const keyword could be used to declare output so it can't be over-written, which would require exclusively using the .push() method to add elements to the output array. Using const when re-assignment isn't needed is a good habit because it can help avoid accidental re-assignment and other bugs.

Alternatively, the function could return early in the base cases - e.g. when n is 1.

\$\endgroup\$
1
  • \$\begingroup\$ Awesome, I appreciate this a lot. I hadn't realized that I only needed one else condition. I'll have to revisit this once I get to learning about const, too! \$\endgroup\$
    – KGE
    Mar 31, 2021 at 5:23

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.