Pascal Triangle Algorithm

In much of the Western universe, it is named after the French mathematician Blaise Pascal, although other mathematicians study it centuries before him in India, Persia (Iran), China, Germany, and Italy. The entry in each row are numbered from the left beginning with K = 0 and are normally staggered relative to the numbers in the adjacent rows. In Italy, Pascal's triangle is referred to as Tartaglia's triangle, named for the Italian algebraist Niccolò Fontana Tartaglia (1500–1577), who published six rows of the triangle in 1556.Gerolamo Cardano, also, published the triangle as well as the additive and multiplicative rules for constructing it in 1570.Pascal's Traité du triangle arithmétiqueThe triangle was later named after Pascal by Pierre Raymond de Montmort (1708) who named it" table de M. Pascal pour les combinaisons"(French: table of Mr. Pascal for combinations) and Abraham de Moivre (1730) who named it" Triangulum Arithmeticum PASCALIANUM"
const numRows = 5

var generate = function (numRows) {
  const triangle = [[1], [1, 1]]

  if (numRows === 0) {
    return []
  } else if (numRows === 1) {
    return [[1]]
  } else if (numRows === 2) {
    return [[1], [1, 1]]
  } else {
    for (let i = 2; i < numRows; i++) {
      addRow(triangle)
    }
  }
  return triangle
}
var addRow = function (triangle) {
  const previous = triangle[triangle.length - 1]
  const newRow = [1]
  for (let i = 0; i < previous.length - 1; i++) {
    const current = previous[i]
    const next = previous[i + 1]
    newRow.push(current + next)
  }
  newRow.push(1)
  return triangle.push(newRow)
}

generate(numRows)

LANGUAGE:

DARK MODE: