diff --git a/algorithms/JavaScript/README.md b/algorithms/JavaScript/README.md index 3034ad0d..132fc3b0 100644 --- a/algorithms/JavaScript/README.md +++ b/algorithms/JavaScript/README.md @@ -1,31 +1,42 @@ # JavaScript ## Arrays + - [Counting Inversions](src/arrays/counting-inversions.js) ## Linked Lists + - [Singly](src/linked-lists/singly.js) - [Doubly](src/linked-lists/doubly.js) ## Searching + - [Binary Search Recursive](src/searching/binary-search-recursive.js) - [Binary Search](src/searching/binary-search.js) - [Linear Search](src/searching/linear-search.js) - + ## Sorting + - [Bubble Sort](src/sorting/bubble-sort.js) - [Insertion Sort](src/sorting/insertion-sort.js) - [Merge Sort](src/sorting/merge-sort.js) - [Quick Sort](src/sorting/quick-sort.js) - [Selection Sort](src/sorting/selection-sort.js) - + ## Strings + - [Palindrome](src/strings/palindrome.js) - [Sequence](src/strings/sequence.js) ## Stacks + - [Stacks](src/stacks/stack.js) - [Two Stack](src/stacks/two-stack.js) ## Queues + - [Queue](src/queues/queue.js) + +## Maths + +- [Fibonacci Series](src/maths/fibonacci-series.js) diff --git a/algorithms/JavaScript/src/index.js b/algorithms/JavaScript/src/index.js index f85a9fe7..806026de 100644 --- a/algorithms/JavaScript/src/index.js +++ b/algorithms/JavaScript/src/index.js @@ -4,6 +4,9 @@ require('./arrays/counting-inversions'); // Linked Lists require('./linked-lists/singly'); +// Maths +require('./maths/fibonacci-series'); + // Searching require('./searching/binary-search-recursive'); require('./searching/binary-search'); @@ -22,7 +25,7 @@ require('./strings/sequence'); // Stack require('./stacks/stack'); -require('./stacks/two-stack') +require('./stacks/two-stack'); // Queue require('./queues/queue'); diff --git a/algorithms/JavaScript/src/maths/fibonacci-series.js b/algorithms/JavaScript/src/maths/fibonacci-series.js new file mode 100644 index 00000000..1321130d --- /dev/null +++ b/algorithms/JavaScript/src/maths/fibonacci-series.js @@ -0,0 +1,24 @@ +// algorithm to generate first n numbers of fibonacci series +// Time complexity: O(n) + +function fibonacci(n) { + // Initialize array 'series' + // with the first two numbers of the fibonacci series [0, 1] + const series = [0, 1]; + for (let i = 3; i <= n; i++) { + // starting from the third number, and stopping at number n + const num1 = series[series.length - 1]; + // num1 is the last number of the series + const num2 = series[series.length - 2]; + // num2 is the second-to-last number of the series + const next = num1 + num2; + // next number is the sum of the last two + series.push(next); + // add number to list + } + // return list + return series; +} + +console.log(fibonacci(10)); +// output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]