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 complexity
pull/757/head
Valerio Cipolla 2022-04-30 17:38:43 +01:00 committed by GitHub
parent aeee10f2f0
commit 6a64805e36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 3 deletions

View File

@ -1,18 +1,22 @@
# 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)
@ -20,12 +24,19 @@
- [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)

View File

@ -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');

View File

@ -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]