2
\$\begingroup\$

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 < num; i++) {
      result[i] = [];
      result[i][0] = 1;

      for (var j = 1; j < i; j++) {

        result[i][j] = result[i - 1][j - 1] + result[i - 1][j];
      }
      result[i][j] = 1
    }
  }
  for (var i = 0; i < 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
\$\endgroup\$
  • \$\begingroup\$ Welcome to Code Review. Since you explicitly ask to change the behavior of your code (to fix the formatting), that part of the question is off-topic for Code Review — but it has been addressed before in a previous answer. \$\endgroup\$ – 200_success May 7 '19 at 3:06
  • \$\begingroup\$ @200_success Hi, I just wanted to clarify for next time. I'm not too sure why this code is not implemented or working as intended. It works but I had some questions about it. So it does fulfill this requirement, right? "We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." Also I did see the question/answer you referred to but I'm still learning and haven't gotten to python yet. Sorry :( \$\endgroup\$ – Baecho May 11 '19 at 1:26
  • \$\begingroup\$ The question specifically asks to fix the formatting to accommodate larger numbers. Therefore the code is not working correctly as intended, and is not ready to be reviewed, according to the rules in the help center. \$\endgroup\$ – 200_success May 11 '19 at 1:28
  • \$\begingroup\$ @200_success Sorry I totally thought I was on Stack Overflow!! Makes sense now! Thank you!! \$\endgroup\$ – Baecho May 11 '19 at 1:33
1
\$\begingroup\$

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 < num; i++) {
    result[i] = [];
    result[i][0] = 1;
    for (var j = 1; j < 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 <= 1) {
    console.log(num);
  }
  var result = generatePascalArrays(num),  
      width = Math.max(...result[result.length-1]).toString().length;  
  for (var i = 0; i < 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) => ' '.repeat(width - x.toString().length) + x));
  }
}

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

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thank you! I haven't quite learned most of the stuff at the end, but I will definitely look it up now! \$\endgroup\$ – Baecho May 11 '19 at 1:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.