DEV Community

Discussion on: Solving Pascal's Triangle in JavaScript

Collapse
 
pemasat profile image
Petr Mašát • Edited

It is nice, but I don't prefer "let" and "for" in javascript, so i tried re-writte it

or with JEST testing and doc in gitlab.com/pemasat/playground/-/bl...

I hope that you will like it.

Collapse
 
alyconr profile image
Jeysson Aly Contreras

I rewrite it in a simplest way

function pascal(rows) {
  if (rows === 1) {
    return [[1]];
  }
if (rows === 0) {
    return [];
  }
  const prev = pascal(rows - 1);
  const last = prev[prev.length - 1];
  const next = [1];
  for (let i = 0; i < last.length - 1; i++) {
    next.push(last[i] + last[i + 1]);
  }
  next.push(1);
  prev.push(next);
  return prev;
}
Enter fullscreen mode Exit fullscreen mode