This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // recursive calculation of Fibonacci numbers | |
| function fibonacci(number) { | |
| if (number <= 1) return number; | |
| return fibonacci(number - 2) + fibonacci(number - 1); | |
| } | |
| console.log(fibonacci(5)); | |
| console.log(fibonacci(6)); | |
| console.log(fibonacci(7)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // read an array of array | |
| function readArrayOfArray() { | |
| // This is an array of arrays. | |
| // It is a little easier to read like this: | |
| var arrayNumberFruit = [ | |
| [1, 'Orange'], | |
| [2, 'Banana'], | |
| [3, 'Apple'], | |
| [4, 'Mango'] | |
| ]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // return true if an item exists in list | |
| // and false if it doesn't | |
| var fruits = ["Banana", "Orange", "Apple", "Mango"]; | |
| var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]; | |
| function checkItemInList(item, list) { | |
| return list.includes(item); | |
| } | |
| console.log('Is Mango in Fruit List:', checkItemInList('Mango', fruits)); // true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // get the lenght of an array list | |
| var fruits = ["Banana", "Orange", "Apple", "Mango"]; | |
| var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]; | |
| function getListLenght(list) { | |
| var _lenght = list.length; | |
| return _lenght; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Binary Search(Iterative Approach) | |
| var binarySearch = function (array, number) { | |
| var start = 0; | |
| var end = array.length - 1; | |
| // Iterate while start not meets end | |
| while (start <= end) { | |
| // Find the mid index |