Javascript fibonocci sequence

I was experimenting and did a fibonocci number generator.
Just wanted to share.

var alist = [0, 1, 1, 2];

const add_it = (number, previous, alist, nth, start) => {
    console.log(" *************  ")
    //console.log(alist)
    let next_number = previous + number;
    let next_start = start + 1
    if(next_start >= nth){
      console.log("-----------    final list  ------------")
      console.log(alist);
    }
    else {
      alist.push(next_number);
      console.log(alist);
      add_it(next_number, number, alist, nth, next_start);
    };
};

add_it(2, 1, alist, 15, 1);
2 Likes

Ah very cool. Can you crash it?

Sure thing. Can get recursion depth by just changing it so the comparison for the (if) never gets met. Forget to increment nth, swap the wrong variables. I think I crashed it many different ways before it worked. Still needs testing and all that. Was experimenting.
:slight_smile: