Skip to content

Instantly share code, notes, and snippets.

@apiv
Last active July 17, 2017 19:09
Show Gist options
  • Save apiv/4399737 to your computer and use it in GitHub Desktop.
Save apiv/4399737 to your computer and use it in GitHub Desktop.
/* the function */
fib = (x) ->
if x < 2
x
else
fib(x-1) + fib(x-2)
/* test it out */
solutions = []
for number in [0..10]
solutions.push ( fib number )
console.log solutions
@aminnairi
Copy link

Note that comments in CoffeeScript are # comment not /* comment */ nor // comment.
Here is a more iterative and concise version:

f = (l) ->
  p = 0
  n = 1
  [0].concat (p for i in [1..l] when [p, n] = [n, p + n])
console.log f 10 # (11) [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

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