complicated pascal triangle using JavaScript I wrote a function to generate a complicated version of the pascal triangle. The generated triangle should look like that:

┌─────────┬───┬───┬────┬────┬────┬────┬────┬───┬───┐
│ (index) │ 0 │ 1 │ 2  │ 3  │ 4  │ 5  │ 6  │ 7 │ 8 │
├─────────┼───┼───┼────┼────┼────┼────┼────┼───┼───┤
│    0    │ 1 │   │    │    │    │    │    │   │   │
│    1    │ 1 │ 1 │ 1  │    │    │    │    │   │   │
│    2    │ 1 │ 2 │ 3  │ 2  │ 1  │    │    │   │   │
│    3    │ 1 │ 3 │ 6  │ 7  │ 6  │ 3  │ 1  │   │   │
│    4    │ 1 │ 4 │ 10 │ 16 │ 19 │ 13 │ 10 │ 4 │ 1 │
└─────────┴───┴───┴────┴────┴────┴────┴────┴───┴───┘


My implementation is like that

function createTriangle (n) {
// initialize the holding matrix
  let matrix = [];

// iterate over the matrix rows, and for each row construct a new array of row number + 1

  for (let row = 0; row &lt; n; row++) { 
    matrix[row] = new Array(row+1);
// iterate over each row and check conditions

    for (let col = 0; col &lt;= 2*row; col++) {            
      if ((col === 0 &amp;&amp; row &gt;= 0) || (col === 2 * row)) {
        matrix[row][col] = 1;
// first case
      } else if(col === row &amp;&amp; col === 1){
                matrix[row][col] = 1;
// second case ;
            } else if(row &gt; col &amp;&amp; matrix[row][col-1]=== 1 ) {
            matrix[row][col] = 
            1 + matrix[row-1][col];
// third case
            } else if(row &gt; col &amp;&amp; matrix[row][col-1]!== 1 ) {
            matrix[row][col] = 
            matrix[row-1][col-2] + matrix[row-1][col-1] + matrix[row-1][col];
// fourth case
     } else if((row === col &amp;&amp; (matrix[row][col-1] !== 1))) {
                matrix[row][col] = 
                matrix[row-1][col-2] + matrix[row-1][col-1] + matrix[row-1][col];

// fifth case   
       // if the col number is even, add only the previous 2 elements in the previous row       
            } else if(row &lt; col &amp;&amp; col % 2 === 0){
                matrix[row][col] = matrix[row-1][col-2] + matrix[row-1][col-1] + matrix[row-1][col];
// sixth case

      // if the col number is odd add the previous 3 elements in the previous row
            } else if(row &lt; col &amp;&amp; col % 2 === 1){

            matrix[row][col] = matrix[row-1][col-2] + matrix[row-1][col-1];
            }
        }
    }
  return matrix;
}


console.table(createTriangle(5))


I feel that my code is a bit confusing. In addition, there is a bug on matrix[4, 5] it should be 16,but it is 13?? Could anyone advise if there is a more refactored elegant solution? Appreciate your time and help.

Edit
The algorithm should generate a matrix that have number of columns twice number of rows. The elements before the element at matrix[row][col] where col = row should be the same but reversed in a desc. order. for example, matrix[4,4] => [1,4,10,16,19,16,10,4,1]. element at matrix[4,4] 19 should be equal to 6+7+6
You are not properly calculating the pascal triangle. Here is my solution, in this code I take advantage of the function .splice() to modify the content of every row. You know that the outter elements of each row are 1s and in between you make the calculations of the sum of the top "root elements", and just like that you get the Pascal's Triangle.



function pascalTriangle(level) {
  let matrix = [[1]];

  for (let row = 1; row &lt; level; row++) {
    matrix.push([1, 1]);
    for (let col = 1; col &lt; row; col++) {
      matrix[row].splice(col, 0, matrix[row - 1][col - 1] + matrix[row - 1][col]);
    }
  }

  return matrix;
}

console.log(pascalTriangle(7));




┌─────────┬───┬───┬────┬────┬────┬───┬───┐
│ (index) │ 0 │ 1 │ 2  │ 3  │ 4  │ 5 │ 6 │
├─────────┼───┼───┼────┼────┼────┼───┼───┤
│    0    │ 1 │   │    │    │    │   │   │
│    1    │ 1 │ 1 │    │    │    │   │   │
│    2    │ 1 │ 2 │ 1  │    │    │   │   │
│    3    │ 1 │ 3 │ 3  │ 1  │    │   │   │
│    4    │ 1 │ 4 │ 6  │ 4  │ 1  │   │   │
│    5    │ 1 │ 5 │ 10 │ 10 │ 5  │ 1 │   │
│    6    │ 1 │ 6 │ 15 │ 20 │ 15 │ 6 │ 1 │
└─────────┴───┴───┴────┴────┴────┴───┴───┘