function fn(n) { for (var i = 0; i <= n; i++) { var arr = []; for (var j = 0; j <= i; j++) { // console.log(i,j) arr.push(combination(i, j)) } console.log(arr) } } function combination(m, n) { if (n == 0) { return 1; } else if (m == n) { return 1; } else { return combination(m - 1, n - 1) + combination(m - 1, n) } } fn(5)