Skip to content

Instantly share code, notes, and snippets.

@creaturenex
Created December 21, 2022 18:27
Show Gist options
  • Save creaturenex/bb2065f2fdb4223a3166f65d5bddfe98 to your computer and use it in GitHub Desktop.
Save creaturenex/bb2065f2fdb4223a3166f65d5bddfe98 to your computer and use it in GitHub Desktop.
recursion problems
// Example 1
// Write a function that returns the factorial of a number.
// EXAMPLE4! = 4 * 3 * 2 * 1 = 24, so calling factorial(4) should return 24.
// Initial example as I did not expect the function to use an argument as storage
// function factorial(num) {
// if (num == 1) return num;
// return num * factorial(num - 1);
// };
function factorial(num, product = 1) {
if (num === 1) return product;
return factorial(num-1, product * num)
};
// To check if you've completed the challenge, uncomment these console.logs!
console.log(factorial(4)); // -> 24
console.log(factorial(6)); // -> 720
// Example 2
// Write a function that takes two inputs, a base and an exponent,
// and returns the expected value of base ^ exponent. For instance, if our base is 2
// and our exponent is 3, then return 8 because 2^3 = 2*2*2 = 8.
// This function initial was difficult because I was trying to do something like,
// return pow(base * base , --exponent), but was having trouble seeing how could
// I keep track on the initial base value. totally forgot that you can perform a
// math operation on the return value of the invoked function.
function pow(base, exponent) {
if (exponent === 1) return base ; // when is it 0 vs 1
return base * pow(base, --exponent)
}
// To check if you've completed the challenge, uncomment these console.logs!
console.log(pow(2, 4)); // -> 16
console.log(pow(3, 5)); // -> 243
Example 3
/*
You are creating a card game application with your friend.
She already wrote a function that divides the deck of cards into top and bottom halves,
but needs help writing a function that shuffles the two halves together again.
Challenge
Write a function that takes two arrays as inputs, representing the top and bottom halves of a deck of cards,
and shuffles them together. The function will return a single array containing the elements from both input
arrays interleaved, like so:
- the first element should be the first element of the first input array,
- the second element should be the first element of the second input array,
- the third element should be the second element of the first input array,
- the fourth element should be the second element of the second array,
- and so on.
The arrays may be of different lengths. After interleaving the elements of the input arrays,
any remaining elements should be appended to the end of the array.
This problem can be solved in many ways, but try to solve it with recursion!
*/
// this code is incorrect, but moved on to see the correct answer
function shuffleCards(topHalf, bottomHalf) {
let subArr = [topHalf[0],bottomHalf[0]]
let topClone = [...topHalf]
let bottomClone = [...bottomHalf]
// is doing 2 extra operations, flattening then filtering
if (topHalf[0] == undefined || bottomHalf[0] == undefined) return topClone.flat().filter(x => x!= undefined)
topClone.push(subArr)
// instead of cloning, I can slice arrays instead, which in affect also shifts
topClone.shift()
bottomClone.shift()
return shuffleCards(topClone, bottomClone)
// I wanted to incorporate this with the return [topHalf[0],bottomHalf[0]] but forgot about concat()
//
}
// UNCOMMENT TO TEST YOUR WORK
const topHalf = ['Queen of Diamonds', 'Five of Hearts', 'Ace of Spades', 'Eight of Clubs'];
const bottomHalf = ['Jack of Hearts', 'Ten of Spades'];
console.log(shuffleCards(topHalf, bottomHalf));
/*-> ['Queen of Diamonds',
'Jack of Hearts',
'Five of Hearts',
'Ten of Spades',
'Ace of Spades',
'Eight of Clubs',
]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment