Created
February 15, 2021 17:52
-
-
Save ManishPoduval/a3eb5e419bbcc319ac767114d706be81 to your computer and use it in GitHub Desktop.
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 anagrams = function(word){ | |
var anagramResult = []; | |
// Base case, word has 1 character. | |
if (word.length === 1) return [word]; | |
// Else, go through every letter of the word | |
for (var i = 0; i < word.length; i++) { | |
var restOfWord = word.substring(0, i) + word.substring(i + 1); | |
var results = anagrams(restOfWord); | |
for (var j = 0; j < results.length; j++) { | |
anagramResult.push(word[i] + results[j]); | |
} | |
} | |
return anagramResult; | |
} | |
console.log(anagrams('cats')) |
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 binary = function(num){ | |
if(num/2<1){ | |
const remainder= num%2 | |
return "1" | |
} | |
else { | |
const remainder= num%2 | |
return remainder+binary(Math.floor(num/2)) | |
} | |
} | |
console.log(binary(25)) |
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 countingSheep = function(numSheep) { | |
if(numSheep===0){ | |
console.log("All sheep jumped over the fence") | |
} | |
else { | |
console.log(`${numSheep}: Another sheep jumped over the fence`) | |
countingSheep(numSheep-1) | |
} | |
} | |
countingSheep(3) |
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 factorial = function(num){ | |
if (num===1){ | |
return 1 | |
} | |
else { | |
return num*factorial(num-1) | |
} | |
} | |
console.log(factorial(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
const fibonacci = function(num){ | |
if (num<=1){ | |
return 1 | |
} | |
else { | |
return fibonacci(num-1)+fibonacci(num-2) | |
} | |
} | |
console.log(fibonacci(4)) |
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 mazeSolve = function(maze,i,j){ | |
if(maze[i][j]==='e'){ | |
let solved = `We found the maze at column ${j} and row ${i}` | |
return solved | |
} | |
//down | |
if(maze[i+1] && maze[i+1][j]!=='*'){ | |
console.log('D') | |
mazeSolve(maze,i+1,j) | |
} | |
//right | |
if(maze[i][j+1] && maze[i][j+1]!=='*'){ | |
console.log('R') | |
mazeSolve(maze,i,j+1) | |
} | |
} | |
let mySmallMaze = [ | |
[' ', ' ', ' '], | |
[' ', '*', ' '], | |
[' ', ' ', 'e'] | |
]; | |
let maze = [ | |
[' ', ' ', ' ', '*', ' ', ' ', ' '], | |
['*', '*', ' ', '*', ' ', '*', ' '], | |
[' ', ' ', ' ', ' ', ' ', ' ', ' '], | |
[' ', '*', '*', '*', '*', '*', ' '], | |
[' ', ' ', ' ', ' ', ' ', ' ', 'e'] | |
]; | |
console.log(mazeSolve(mySmallMaze,0,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
const powerCalculator = function(base,exponent){ | |
if(exponent<0){ | |
return 'exponent should be >= 0' | |
} | |
else if(exponent===0){ | |
return 1 | |
} | |
else { | |
return base*powerCalculator(base,exponent-1) | |
} | |
} | |
console.log(powerCalculator(3,3)) |
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 reverseString = function(string){ | |
if (string === ''){ | |
return '' | |
} | |
else { | |
return reverseString(string.substr(1))+ string.charAt(0) | |
} | |
} | |
console.log(reverseString('hello')) |
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 stringSplitter = function(string,separator){ | |
const results = [] | |
if(){} //take out, just there for error | |
if(string.indexOf(separator)===-1){ | |
results.push(string) | |
console.log(string) | |
return results | |
} | |
else{ | |
const index = string.indexOf(separator) | |
let beginningWord = string.substring(0,index) | |
console.log(beginningWord) | |
results.push(beginningWord) | |
let restOfWord = string.substring(index+1, string.length) | |
stringSplitter(restOfWord,separator) | |
} | |
return results | |
} | |
console.log(stringSplitter("02/15/2020","/")) | |
// it is breaking up the string how i want it, | |
// but having hard time getting to push onto results |
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 triangularNumber = function(nth){ | |
if (nth<=1){ | |
return nth | |
} | |
else { | |
return nth + triangularNumber(nth-1) | |
} | |
} | |
console.log(triangularNumber(2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment