Skip to content

Instantly share code, notes, and snippets.

@alexhawkins
Last active February 21, 2020 23:53
Show Gist options
  • Select an option

  • Save alexhawkins/287981e27b3649d3f72a to your computer and use it in GitHub Desktop.

Select an option

Save alexhawkins/287981e27b3649d3f72a to your computer and use it in GitHub Desktop.
Hack Reactor Sample Recursion Exercises
'use strict';
//######### EXAMPLE 1 #################/
// Implement function countDecrementing that takes a number `n` and logs numbers n to 0
var countDown = function(num) {
if (num >= 0) {
console.log(num);
countDown(num - 1);
}
};
//tests
countDown(5); //5 4 3 2 1
//######### EXAMPLE 2 #################/
// Implement function countIncrementing that takes a number `n` and logs numbers 0 to n
var countUp = function(num) {
if (num >= 0) {
countUp(num - 1);
console.log(num);
}
};
//tests
countUp(5); //1 2 3 4 5
console.log('*********');
//######### EXAMPLE 3 #################/
// Implement function sum that takes a number `n` and returns the sum of numbers 0 through n
var sumN = function(num) {
return (num > 0) ? num + sumN(num - 1) : 0;
};
//tests
console.log(sumN(5)); // returns 5 + 4 + 3 + 2 + 1 = 15;
console.log(sumN(28));
//######### EXAMPLE 4 #################/
// Implement function exponent that takes a base and exponent and returns base to the power of expo
var exponent = function(base, exp) {
return (exp > 0) ? base * exponent(base, exp - 1) : 1;
};
//tests
console.log(exponent(2, 3)); // 8
console.log(exponent(3, 3)); // 27
console.log(exponent(3, 2)); // 9
console.log(exponent(4, 5)); // 1024
//######### EXAMPLE 5 #################/
// Implement function factorial that takes a number n and returns the factorial of n
var factorial = function(num) {
return (num > 0) ? num * factorial(num - 1) : 1;
};
//tests
console.log(factorial(3)); // 6
console.log(factorial(5)); // 120
console.log(factorial(10)); // 3628800
console.log(factorial(20)); // 2432902008176640000
//######### EXAMPLE 6 #################/
/********************************************************************************************/
// Implement function reverseString that takes a string and returns a reversed version of the string
//the old fashioned way
var reverseString = function(str) {
var s = '';
for (var i = str.length - 1; i >= 0; i--)
s += str[i];
return s;
};
//tests
console.log('dogman = ' + reverseString('dogman')); //namgod
//RECURSIVELY
var reverseStr = function(str) {
return (str.length > 0) ? str[str.length - 1] + reverseStr(str.substr(0, str.length - 1)) : '';
};
//tests
console.log(reverseStr('setab retsam')); //master bates
console.log("Zander".substr(0, "Zander".length - 1));
//######### EXAMPLE 8 #################
// Implement a recursive function called copy that takes an array of arrays made of either [number, array] or [number, null]
// return the number of arrays including the given array
// Example: [100, [200, [300, [400, null]]]] returns 4"
function copy(arr){
var numArray = 1;
arr.forEach(function(element){
if (Array.isArray(element))
numArray += copy(element);
});
return numArray;
}
console.log(copy([100, [200, [300, [400, null]]]]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment