I have written the printing staircase algorithm. A function that given n prints the staircase n levels.
var i = 1;
function printStaircase(n) {
//Base case
if (n < 1) return n;
//Recursive case
var line = '';
line += ' '.repeat(n - 1);
line += '*'.repeat(i);
console.log(line);
i++;
return printStaircase(n - 1);
}
printStaircase(10);
As you can see I have to pass in the i variable from the outside. I'm wondering how I can accomplish while computing the value of i inside the function body, so that it is self-contained and gets nothing from the global scope
i? If you callprintStaircasefor the second time it might start off with it being12?ifromn. Is it possible?