Pascal's triangle JS formatting For my code, up to the 5th row, the formatting of my triangle is fine. But once I hit the 6th row, the formatting turns funny because I start having double digits. Is there any way to fix this?

Also is my code optimized? Any way I can make it better?

Thanks!

function pascals(num) {
  var result = [[1],[1,1]];

  if (num === 0) {
    console.log(0);
  }

  if (num === 1) {
    console.log(1);
  }

  else {
    for (var i = 2; i &lt; num; i++) {
      result[i] = [];
      result[i][0] = 1;

      for (var j = 1; j &lt; i; j++) {

        result[i][j] = result[i - 1][j - 1] + result[i - 1][j];
      }
      result[i][j] = 1
    }
  }
  for (var i = 0; i &lt; result.length; i++) {
    console.log(' '.repeat(result.length - i) + result[i]);
  }
}

pascals(6)


      1
     1,1
    1,2,1
   1,3,3,1
  1,4,6,4,1
 1,5,10,10,5,1
Multi-digit number is what is throwing it off. I typically fix this by opting to find the highest number and then offset the rest of them appropriately.  I did this back when I was messing around with polynomial triangles.

Fixing the spacing issue

I started by getting the longest possible number and then prepending the space before it to make it line up properly.

Nextly, you have to fix the preappend so it looks better over size 2.  This is accomplished over a fairly odd algorithm.

As for the rest of the code, I did not have time to look at it, but will in a bit.

Fixing structure

Overall, your code looked pretty good.  Here are just some suggestions.

I went ahead and moved the pascal array generation to another function.  The idea of a function is that it performs a task.  Logically breaking up these tasks into multiple functions limits the size of each function and increases readability.  From there, we can assign it to a variable inside the pascal function.  I used the intuitive name generatePascalArrays, so you can get the gist of what the function does from the variable assignment.

Secondly, I noticed you split the 1 and 0 exception cases, I combined that into one if and console.log(num).

Additionally, I did use some arrow functions.  In other languages, you may have heard of them as lambda functions.  If you are not familiar with them, here is a link.



function generatePascalArrays(num) {
  var result = [[1],[1,1]];
  for (var i = 2; i &lt; num; i++) {
    result[i] = [];
    result[i][0] = 1;
    for (var j = 1; j &lt; i; j++) {
      result[i][j] = result[i - 1][j - 1] + result[i - 1][j];
    }
    result[i][j] = 1;
  }
  return result;
}

function pascals(num) {  
  if (num &lt;= 1) {
    console.log(num);
  }
  var result = generatePascalArrays(num),  
      width = Math.max(...result[result.length-1]).toString().length;  
  for (var i = 0; i &lt; result.length; i++) {
 let preOffsetter = result[result.length-i-1].length * Math.floor(width/3) + result.length - i
 console.log(' '.repeat(preOffsetter) + result[i].map((x) =&gt; ' '.repeat(width - x.toString().length) + x));
  }
}

pascals(5)
pascals(6)
pascals(10)