chore(Javascript): add fibonacci series (#752)
* Add fibonacci series for javascript * Added doc in js readme for fibonacci series * Add user-friendly description of algorithm in comments * fix change fibs to series * fix output spelling * add time complexitypull/757/head
parent
aeee10f2f0
commit
6a64805e36
|
@ -1,18 +1,22 @@
|
||||||
# JavaScript
|
# JavaScript
|
||||||
|
|
||||||
## Arrays
|
## Arrays
|
||||||
|
|
||||||
- [Counting Inversions](src/arrays/counting-inversions.js)
|
- [Counting Inversions](src/arrays/counting-inversions.js)
|
||||||
|
|
||||||
## Linked Lists
|
## Linked Lists
|
||||||
|
|
||||||
- [Singly](src/linked-lists/singly.js)
|
- [Singly](src/linked-lists/singly.js)
|
||||||
- [Doubly](src/linked-lists/doubly.js)
|
- [Doubly](src/linked-lists/doubly.js)
|
||||||
|
|
||||||
## Searching
|
## Searching
|
||||||
|
|
||||||
- [Binary Search Recursive](src/searching/binary-search-recursive.js)
|
- [Binary Search Recursive](src/searching/binary-search-recursive.js)
|
||||||
- [Binary Search](src/searching/binary-search.js)
|
- [Binary Search](src/searching/binary-search.js)
|
||||||
- [Linear Search](src/searching/linear-search.js)
|
- [Linear Search](src/searching/linear-search.js)
|
||||||
|
|
||||||
## Sorting
|
## Sorting
|
||||||
|
|
||||||
- [Bubble Sort](src/sorting/bubble-sort.js)
|
- [Bubble Sort](src/sorting/bubble-sort.js)
|
||||||
- [Insertion Sort](src/sorting/insertion-sort.js)
|
- [Insertion Sort](src/sorting/insertion-sort.js)
|
||||||
- [Merge Sort](src/sorting/merge-sort.js)
|
- [Merge Sort](src/sorting/merge-sort.js)
|
||||||
|
@ -20,12 +24,19 @@
|
||||||
- [Selection Sort](src/sorting/selection-sort.js)
|
- [Selection Sort](src/sorting/selection-sort.js)
|
||||||
|
|
||||||
## Strings
|
## Strings
|
||||||
|
|
||||||
- [Palindrome](src/strings/palindrome.js)
|
- [Palindrome](src/strings/palindrome.js)
|
||||||
- [Sequence](src/strings/sequence.js)
|
- [Sequence](src/strings/sequence.js)
|
||||||
|
|
||||||
## Stacks
|
## Stacks
|
||||||
|
|
||||||
- [Stacks](src/stacks/stack.js)
|
- [Stacks](src/stacks/stack.js)
|
||||||
- [Two Stack](src/stacks/two-stack.js)
|
- [Two Stack](src/stacks/two-stack.js)
|
||||||
|
|
||||||
## Queues
|
## Queues
|
||||||
|
|
||||||
- [Queue](src/queues/queue.js)
|
- [Queue](src/queues/queue.js)
|
||||||
|
|
||||||
|
## Maths
|
||||||
|
|
||||||
|
- [Fibonacci Series](src/maths/fibonacci-series.js)
|
||||||
|
|
|
@ -4,6 +4,9 @@ require('./arrays/counting-inversions');
|
||||||
// Linked Lists
|
// Linked Lists
|
||||||
require('./linked-lists/singly');
|
require('./linked-lists/singly');
|
||||||
|
|
||||||
|
// Maths
|
||||||
|
require('./maths/fibonacci-series');
|
||||||
|
|
||||||
// Searching
|
// Searching
|
||||||
require('./searching/binary-search-recursive');
|
require('./searching/binary-search-recursive');
|
||||||
require('./searching/binary-search');
|
require('./searching/binary-search');
|
||||||
|
@ -22,7 +25,7 @@ require('./strings/sequence');
|
||||||
|
|
||||||
// Stack
|
// Stack
|
||||||
require('./stacks/stack');
|
require('./stacks/stack');
|
||||||
require('./stacks/two-stack')
|
require('./stacks/two-stack');
|
||||||
|
|
||||||
// Queue
|
// Queue
|
||||||
require('./queues/queue');
|
require('./queues/queue');
|
||||||
|
|
|
@ -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]
|
Loading…
Reference in New Issue