Skip to content

Instantly share code, notes, and snippets.

@ManishPoduval
Created February 15, 2021 17:52
Show Gist options
  • Save ManishPoduval/a3eb5e419bbcc319ac767114d706be81 to your computer and use it in GitHub Desktop.
Save ManishPoduval/a3eb5e419bbcc319ac767114d706be81 to your computer and use it in GitHub Desktop.
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'))
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))
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)
const factorial = function(num){
if (num===1){
return 1
}
else {
return num*factorial(num-1)
}
}
console.log(factorial(5))
const fibonacci = function(num){
if (num<=1){
return 1
}
else {
return fibonacci(num-1)+fibonacci(num-2)
}
}
console.log(fibonacci(4))
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))
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))
const reverseString = function(string){
if (string === ''){
return ''
}
else {
return reverseString(string.substr(1))+ string.charAt(0)
}
}
console.log(reverseString('hello'))
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
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