CSD 360 - JS Fibonacci Sequence

Mod 6 Assignment

This assignment utilizes JavaScript functions to display the first 30 numbers in the Fibonacci sequence, leaving off 0 and starting with 1. The Fibonacci sequence is a series of numbers where each number is the sum of the two preceeding numbers. Here are three approaches that can be used to calculate the the Fibonacci sequence numbers such as using a recursive function, a forloop function, or a whileloop function. Each method uses a different implementation of the Fibonacci algorithm to calculate the numbers but still produces same result. Additonally, another forloop function is used to display the entire Fibonacci sequence up to 30. Where each iteration calls one of the previously specified methods passing in the current iteration index as a paremeter to return the calculated Fibonacci number for the index.


Recursive Fibonacci Function

This function generates the Fibonacci sequence using recursion. It takes an index as input and returns the Fibonacci number at that index. If the index is less than 2, it simply returns the index itself. Otherwise, it calls itself recursively with the previous two indices and returns the sum of those two calls. In terms of efficiency, this recursive function is less efficient than using an iterative approach such as a forloop or whileloop. Because there is increased overhead with multiple function calls in terms of memory allocation and execution time. As the input size increases, the number of function calls increases exponentially.

Forloop Fibonacci Function

This function generates the Fibonacci sequence using a for loop. It takes an index as input and returns the Fibonacci number at that index. If the index is 1 or 2, it simply returns 1. Otherwise, it initializes two variables to 1 and uses a for loop to calculate the Fibonacci number at the given index by repeatedly adding the previous two numbers in the sequence. In terms of efficiency, this method is more efficient than a recursive method as it avoids the overhead of function calls. It is also slightly less verbose than using a whileloop method.

Whileloop Fibonacci Function

This function generates the Fibonacci sequence using a while loop. It takes an index as input and returns the Fibonacci number at that index. If the index is 1 or 2, it simply returns 1. Otherwise, it initializes two variables to 1 and uses a while loop to calculate the Fibonacci number at the given index by repeatedly adding the previous two numbers in the sequence. In terms of efficiency, this method is more efficient than a recursive method. Although it can be slightly more verbose than using a forloop method.


Method Results...