This file contains 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 fib(n) { | |
// Base case | |
if (n <= 2) return 1; | |
// Recursive case | |
return fib(n - 2) + fib(n - 1); | |
} | |
/* | |
ALTERNATIVE LOOP SOLUTION | |
This file contains 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 recursiveRange(num) { | |
if (num === 0) return 0; | |
return num + recursiveRange(num - 1); | |
} | |
// SAMPLE INPUT/OUTPUT | |
// Accepts a number and adds up all the numbers from 0 to number passed in function | |
// recursiveRange(6) // 21 | |
// recursiveRange(10) // 55 |
This file contains 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 productOfArray(arr) { | |
// Base case | |
if (arr.length === 1) return arr; | |
// Recursive case | |
return arr[0] * productOfArray(arr.slice(1)); | |
} | |
// productOfArray([1,2,3]) // 6 | |
// productOfArray([1,2,3,10]) // 60 |
This file contains 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 factorial(num) { | |
// Base case | |
// Edge case for 0! | |
if (num <= 1) return 1; | |
// Recursive case | |
return num * factorial(num - 1); | |
} | |
// 4! = 4 * 3 * 2 * 1 | |
// factorial(1) // 1 |
This file contains 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 power(num, pow) { | |
if (pow === 0) return 1; | |
return power(num, pow - 1) * num; | |
} | |
// Find num^power | |
// power(2,0) // 1 | |
// power(2,2) // 4 | |
// power(2,4) // 16 |
This file contains 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 chunkArrayInGroups(arr, size) { | |
// Base condition | |
if (arr.length <= size) return [arr]; | |
// Recursive case | |
return [arr.slice(0, size), ...chunkArrayInGroups(arr.slice(size), size)] | |
} | |
chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 2); // [[0, 1], [2, 3], [4, 5], [6]] |
This file contains 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 findLongestSubstring(str) { | |
if (str.length === 0) return 0; | |
let maxLen = -Infinity; | |
let start = 0; | |
let end = 0; | |
let strObj = {}; | |
while (start < str.length) { | |
const endLetter = str[end]; |
This file contains 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 minSubArrayLen(nums, sum) { | |
let total = 0; | |
let start = 0; | |
let end = 0; | |
let minLen = Infinity; | |
while (start < nums.length) { | |
if(total < sum && end < nums.length){ | |
total += nums[end]; | |
end++; |
This file contains 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 maxSubarraySum(arr, window){ | |
if (arr.length < window) return null; | |
let tempSum = 0; | |
for (let x = 0; x < window; x++) { | |
tempSum += arr[x] | |
} | |
let maxSum = tempSum; |
This file contains 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 isSubsequence(first, second) { | |
if (first.length > second.length) return false; | |
if (!first) return true; | |
let i = 0; | |
let j = 0; | |
while (j < second.length) { | |
if (first[i] === second[j]) i++; |