0

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

6
  • Is it required to accomplish the output using a recursive function only ? Commented Jun 10, 2019 at 20:43
  • Yes, I don't want to achieve it iteratively. Commented Jun 10, 2019 at 20:43
  • 1
    What about i? If you call printStaircase for the second time it might start off with it being 12? Commented Jun 10, 2019 at 20:44
  • I'm just wondering if it's possible to derive the value of i from n. Is it possible? Commented Jun 10, 2019 at 20:45
  • you can start calling the method like this - printStaircase(10,1); and pass the value of i by incrementing in the next call. Commented Jun 10, 2019 at 20:46

4 Answers 4

2

Recursion is super fun -

const chars = (c = "") => (n = 0) =>
  n === 0
    ? ""
    : c + chars (c) (n - 1)

const spaces =
  chars (" ")

const stars = 
  chars ("*")

const newline =
  "\n"

const stairs = (n, m = n - 1) =>
  m <= 0
    ? ""
    : spaces (m)
      + stars (n - m)
      + newline
      + stairs (n, m - 1)
      
console .log (stairs (10))

console .log (stairs (4))

Sign up to request clarification or add additional context in comments.

Comments

1

n and i are related in that i is simply the inital value of n minus the current value of n +1, so we can capture that quite nicely with something like:

function printStaircase(n) {
  staircaseInternal(n);

  function staircaseInternal(curr) {
    //Base case
    if (curr < 1) return;
    //Recursive case
    var line = ' '.repeat(curr - 1);
    line += '*'.repeat((n - curr) + 1);
    console.log(line);
    staircaseInternal(curr - 1);
  }
}

printStaircase(10);

1 Comment

Nice, so the answer I guess would be that the only way to do this is via a closure or a second param to the function. Was just trying to see how to preserve the initial value of N, thanks this helps.
1

I think something like this would work

function printStaircase(n, i) {
  //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, i);
}
printStaircase(10, 1);

Hope this helps!

2 Comments

Yea, thanks. This is what I also came up with as well
@JohnSnow you can use a default argument function printStairCase(n, i=1) { ... } so when you call printStairCase(10) default value of 1 will be assigned to i
1

Closure to the rescue:

/** 
  * @return stair case n high
  */
function staircase(n) {
  function helper (cur, str) {
    if (cur < 1) return str;
    return helper(
      cur - 1,
      `${str}${' '.repeat(cur)}${'*'.repeat(n-cur+1)}\n`);
  }
  return helper(n, '');
}

/**
  * prints a staircase n-1 hight
  * @return 0 (don't know why)
  */
function printStaircase(n) {
  console.log(staircase(n));
  return 0;
}

printStaircase(10);
printStaircase(3);

2 Comments

The snippet doens't seem to output the correct result
@JohnSnow Fixed now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.