Skip to content

Instantly share code, notes, and snippets.

@nkabrown
Last active July 29, 2016 18:08
Show Gist options
  • Save nkabrown/122ce05a595f192b5c909b1bf13f36a5 to your computer and use it in GitHub Desktop.
Save nkabrown/122ce05a595f192b5c909b1bf13f36a5 to your computer and use it in GitHub Desktop.
C4Q Access Code — Application Workshop

Write a function that takes in an integer and returns an array of that size filled with the Fibonacci sequence.

function fibonacci(size) {
  // create an empty array to store our fibonacci sequence values
  var fibArray = [];
  
  // iterate the value of the size argument
  for (var i = 0; i < size; i++) {
    // if the array contains less than two elements push the value 1 into the array
    if (fibArray.length < 2) {
      fibArray.push(1);
    } else {
      // add together the values of the last two elements in the array
      var sum = fibArray[i - 1] + fibArray[i - 2];
      fibArray.push(sum);
    }
  }
  return fibArray;
}

fibonacci(10);
//=> [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
fibonacci(4);
//=> [1, 1, 2, 3]
@marsikeda
Copy link

Thanks, Nate!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment