Skip to content

Instantly share code, notes, and snippets.

@landonconover
Created September 9, 2020 02:03
Show Gist options
  • Select an option

  • Save landonconover/0ad81a99e5ff54af046119016e9f5da5 to your computer and use it in GitHub Desktop.

Select an option

Save landonconover/0ad81a99e5ff54af046119016e9f5da5 to your computer and use it in GitHub Desktop.
function countDownloop(num) {
while(num >= 0) {
console.log(num)
num--
}
}
countDownloop(4)
function countDown( num ) {
num
if(num <= 0) return
countDown(num - 1)
}
countDown(5)
function factorial(num) {
if(num < 0) return
//base case
if(num === 0) return 1;
//recurisive call
return num * factorial(num - 1);
}
factorial(3) //?
// 1
// 1 * factorial(0)
// 2 * factorial(1)
// 3 * factorial(2)
// 3*2*1*1
// - factorial(0) returns 1
// - factorial(1) returns 1 * factorial(0), or just 1*1
// - factorial(2) returns 2 * factorial(1), or just 2*1*1
// - factorial(3) returns 3 * factorial(2), or just 3*2*1*1
function revStr (str) {
if(str === '') return ''
return revStr(str.substr(1)) + str[0];
}
'cat'.substr(1) //?
revStr('cat') //?
// revStr('cat')
// revStr('at') + c
// revStr('t;) + a
// revStr('') + ''
// - return revStr('at') + 'c'
// - revStr('cat') returns revStr('at') + 'c'
// - revStr('at') returns revStr('t') + 'a'
// - revStr('t') returns revStr('') + 't'
// - revStr('') returns ''
// - Another way to look at it
// - revStr('') returns '' => ''
// - revStr('t') returns revStr('') + 't' => '' + 't'
// - revStr('at') returns revStr('t') + 'a' => '' + 't' + 'a'
// - revStr('cat') returns revStr('at') + 'c' => '' + 't' + 'a' + 'c'
//tac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment