Pascal Triangle — JavaScript Solution

Aniebiet Afia
4 min readMay 30, 2023

--

Pascal’s Triangle is an arrangement of numbers in a triangular array such that the numbers at the beginning and end of each array are 1 and the remaining numbers are the sum of the nearest two numbers in the preceding row.

JavaScript Code to solve Pascal Triangle

function pascalTriangle(n) {
// check if parameter is less than or equal to 0. Return empty array
if (n <= 0) {
return [];
}

// set the outer array to an empty array.
const triangle = [];

// start of the outer loop, where i is less than n
for (let i = 0; i < n; i++) {
// set the row to an empty array
const row = [];

// start the inner loop, where j is less than or equal to i
for (let j = 0; j <= i; j++) {
// checks to set the numbers at beginning and end of row array to 1
if (j === 0 || j === i) {
row.push(1);
} else {
// sums the items between 1
const prevRow = triangle[i - 1];
const sum = prevRow[j - 1] + prevRow[j];
row.push(sum);
}
} //end of inner for loop
// push the row array into the triangle
triangle.push(row);
}
//end of outer for loop

//returns the populated triangle
return triangle;
}

//call the function and pass in the parameter 3
pascalTriangle(3)

Analysis of the function

We define a function named pascalTriangle which takes in an argument n — you can name it whatever you want. For the function above, we call the function and pass in 3 as a parameter. The ouput will be:

[[1], [1, 1], [1, 2, 1]]

The function first checks if the parameter is less than or equal to 0 ie n <= 0 . This will return an empty array [].

If the parameter is greater than 0, it sets the outer array to an empty array [] which will be populated later. This is assigned to the variable triangle.

const triangle = []

The rows of the pascal triangle is set using the outer for loop. The numbers in each of the arrays in the rows are set using the inner for-loop.

The outer for-loop starts at 0 and continues as long as it is less than n . When i = 0, we assign an empty array to the variable row const row = [] and then proceed to the inner loop where j = 0.This executes the if block , if (j === 0 || j === i). This is the same as if (0 === 0 || 0 === 0, which returns true, 1 is pushed into the row array row.push(1). j is increased to 1 and checks against the if-block, it returns false. The inner loop ends and row is pushed into the triangle. The output will look like this:

[[1]]

The outer loop is increased to 1, the inner loop starts at 0 and compares against the if-block.

if (j === 0 || j === i) is the same as if (0 === 0 || 0 === 1). This will return true. The second row is set and 1 is push to the beginning of the array, j is increased to 1, the checks are run again and returns true, 1 is pushed to the end of the array, j is then increased to 2 and the condition is checked and returns false. The inner loop is ended — row is then pushed into the triangle array.

The output becomes [[1], [1, 1]]

i is then increased to 2, this sets the third row, the inner loop starts at j = 0 and checks against the condition which returns true to push 1 to the beginning of the array. j becomes 1 and the checks returns false which executes the else-block. In the else-block, we set prevRow = triangle[i — 1]. Remember that i is 2. Therefore, the code reads const prevRow = triangle[2 — 1] which will return triangle[1] which is triangle at the index of 1. Remember that at this stage, the output of triangle is [[1], [1, 1]], therefore, triangle[1] will return [1, 1].

Next, we sum the numbers;

const sum = prevRow[j — 1] + prevRow[j]. This is the same as const sum = prevRow[1-1] + prevRow[1]. This will return const sum = prevRow[0] + prevRow[1]. This is equivalent to const sum = 1 + 1 === 2. Therefore, 2 is pushed into the row array and j is increased to 2 which checks against the condition and returns true, 1 is then pushed to the end of that row array. The loop ends because j is now 3. The row array is pushed into the triangle array and i becomes 3 which is not less than 3. Hence, the outer loop ends and returns the final triangle array. The output becomes:

[[1], [1, 1], [1, 2, 1]]

I hope you enjoyed this article.

--

--

Aniebiet Afia

I build scalable, high-performance web applications using Node.js. I have a deep understanding of the fundamental concepts of software engineering.