Skip to content

Instantly share code, notes, and snippets.

@lcanady
Created August 15, 2021 19:12
Show Gist options
  • Select an option

  • Save lcanady/9b9cef4670ce0adc3e58c051363920d6 to your computer and use it in GitHub Desktop.

Select an option

Save lcanady/9b9cef4670ce0adc3e58c051363920d6 to your computer and use it in GitHub Desktop.
// stairs = 4
// fun(stairs=4, curr = stairs)
// Start recursion
// we need to check for base case of recursion.
// if we hit the base case, then return a empty string.
// > if hits 0 return blank string
// base case: curr < 1 return ""
// I need to work from max to min 4 to 1. Could also go 1 to 4.
// => dec
// 4 (4,4)
// 3 (4,3)
// 2 (4,2)
// 1 (4,1)
// 0 (4,0) <base Case> no value return; return "" + func()
// 4 sss# \n
// 3 ss## \n
// 2 s### \n
// 1 ####
// return string
// returns curr number of stairs with (stairs - curr) spaces
// dec curr
// curr --
// print spaces
// spaces = curr
// print curr steps
// # x stairs - curr
// check to see if we need to add a return. If curr is
// greater than 0, then there is another line.
// curr > 0 ? \n : "" <== really test this, if needs nl
// call recursion
// > call fun(stairs, curr)
// End recursion
const fun = (stairs, curr = stairs) => {
// base case
if (curr < 1) return "";
// dec curr
curr--;
return (
// print spaces
" ".repeat(curr) +
// print curr steps
"#".repeat(stairs - curr) +
// check for newline
(curr > 0 ? "\n" : "") +
// recursion!!
fun(stairs, curr)
);
};
console.log(fun(4));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment