BACK

Pascal's triangle is a triangular array constructed by summing adjacent elements in preceding rows. Pascal's triangle contains the values of the binomial coefficient. It is named after the 17th century French mathematician, Blaise Pascal (1623 - 1662).



		
var generate = function(numRows) {

	//make array of length numRows
	let rowsArray = [];
	
		//   make the rows - for loop - each iteration appends a row to rowsArray
		for (i = 0; i <= numRows-1; i++ ){
		let newRow = [i];
		newRow[0] = 1;
		newRow[i] = 1;
		rowsArray[i] = newRow;
		};

		for (j = 0; j <= numRows-1; j++){
		// calculate each number based on the previous row - first and last are always 1
		for (k = 0; k <= j; k++){
			//skip first iteration
			if (j >= 1){
			// skip first and last numbers
			if (k != 0 && k != j){
				rowsArray[j][k] = rowsArray[j-1][k] + rowsArray[j-1][k-1];
			}
			}
		};
		};
	return rowsArray;
};