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 recursiveFactorial(num) { | |
if (num === 0 || num === 1) { | |
return 1; | |
} | |
return num * recursiveFactorial(num - 1); | |
} | |
console.log(recursiveFactorial(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 iterativeFactorial(num) { | |
if (num === 0 || num === 1) { | |
return 1; | |
} | |
for (let i = num - 1; i >= 1; i--) { | |
num = num * i; | |
} | |
return num; |
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) { | |
const result = [0, 1]; | |
for (let i = 2; i <= n; i++) { | |
let a = result[i - 1]; | |
let b = result[i - 2]; | |
result.push(a + b); | |
} |
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) { | |
if (n < 2) { | |
return n; | |
} | |
return fib(n - 1) + fib(n - 2); | |
} | |
console.log(fib(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 linearSearch(arr, item) { | |
for (const element of arr) { | |
if (element === item) { | |
return "Found"; | |
} | |
} | |
return "Not Found"; | |
} |
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 binarySearch(arr, item) { | |
let startIndex = 0; | |
let endIndex = arr.length - 1; | |
while (startIndex < endIndex) { | |
let middleIndex = Math.floor((startIndex + endIndex) / 2); | |
if (arr[middleIndex] === item) { | |
return `Found at index ${middleIndex}`; | |
} |
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 bubbleSort(arr) { | |
//sort the array | |
for (let i = 0; i < arr.length; i++) { | |
for (let j = 0; j < arr.length - i - 1; j++) { | |
if (arr[j] > arr[j + 1]) { | |
const lesser = arr[j + 1]; | |
arr[j + 1] = arr[j]; | |
arr[j] = lesser; | |
} | |
} |
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 selectionSort(arr) { | |
for (let i = 0; i < arr.length; i++) { | |
let minIndex = i; | |
for (let j = i + 1; j < arr.length; j++) { | |
if (arr[j] < arr[minIndex]) { | |
minIndex = j; | |
} | |
} |
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 insertionSort(arr) { | |
const len = arr.length; | |
for (let i = 0; i < len; i++) { | |
let key = arr[i]; | |
let j = i - 1; | |
while (j >= 0 && arr[j] > key) { | |
arr[j + 1] = arr[j]; | |
j--; |