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
| function fill(times, number) { | |
| if (times <= 0) return []; | |
| return [number].concat(fill(times -1, number)); | |
| } | |
| console.log(fill(5, 2)); // [2, 2, 2, 2, 2] |
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
| function recLen(str) { | |
| if (str == '') return 0 | |
| return recLen(str.substring(1)) + 1; | |
| } | |
| let str ="abcd"; | |
| console.log(recLen(str)); |
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
| function getElementsByClassName(className) { | |
| const results = []; | |
| const checkRecursive = (element, cName) => { | |
| const cn = element.getAttribute("class"); | |
| if (cn !== undefined && cn !== null && cn.includes(cName)) { | |
| results.push(element); | |
| } | |
| // element.children MDN | |
| if (element.children.length > 0) { |
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
| function flatten(arr, ret = []) { | |
| for (const entry of arr) { | |
| if (Array.isArray(entry)) { | |
| flatten(entry, ret) | |
| } else { | |
| ret.push(entry) | |
| } | |
| } | |
| return ret; | |
| } |
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
| const findFibonacci = (n) => { | |
| if (n == 1) return [0, 1]; | |
| let res = findFibonacci(n-1); | |
| res.push(res[res.length - 1] + res[res.length - 2]) | |
| return res; | |
| } | |
| console.log(findFibonacci(5)); // [0, 1, 1, 2, 3, 5] |
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
| // recursion | |
| const recursiveFindRange = (low, high) => { | |
| if (low === high - 2) return [high - 1]; | |
| let res = recursiveFindRange(low, high - 1) | |
| res.push(high - 1); | |
| return res; | |
| }; |
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
| const recursiveDigitSum = (number) => { | |
| if (number === 0) return 0; | |
| return number % 10 + recursiveDigitSum(Math.floor(number / 10)) | |
| } |
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
| const factorialNumber = (num) => { | |
| if (num === 1) return 1; | |
| return num * factorialNumber(num - 1); | |
| } | |
| console.log(factorialNumber(5)); // 120 |
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
| function fibonacci(num) { | |
| if(num < 2) { | |
| return num; | |
| } | |
| else { | |
| return fibonacci(num-1) + fibonacci(num - 2); | |
| } | |
| } |
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
| // recursion 1 - using helper function | |
| const RecursiveHelperProductArr = (arr) => { | |
| let index = arr.length - 1; | |
| // helper function | |
| const recur = (curIndex, acc) => { | |
| acc *= arr[curIndex]; | |
| if (curIndex === 0) return acc; | |
| return recur(curIndex - 1, acc); // decrement the index and recursion | |
| } |
NewerOlder