Pascal's triangle: Difference between revisions

Line 1,868:
 
=={{header|JavaScript}}==
 
===Imperative===
 
{{works with|SpiderMonkey}}
{{works with|V8}}
Line 1,956 ⟶ 1,959:
1 7 15 23 23 15 7 1
</pre>
 
===Functional (ES5)===
 
<lang JavaScript>(function (n) {
 
// A Pascal triangle of n rows
// n --> [[n]]
function pascalTriangle(n) {
 
// Sums of each consecutive pair of numbers
// [n] --> [n]
function pairSums(lst) {
return lst.reduce(function (acc, n, i, l) {
var iPrev = i ? i - 1 : 0;
return i ? acc.concat(l[iPrev] + l[i]) : acc
}, []);
}
 
// Next line in a Pascal triangle series
// [n] --> [n]
function nextPascal(lst) {
return lst.length ? [1].concat(
pairSums(lst)
).concat(1) : [1];
}
 
// Each row is a function of the preceding row
return n ? Array.apply(null, Array(n - 1)).reduce(function (a, _, i) {
return a.concat([nextPascal(a[i])]);
}, [[1]]) : [];
}
 
// TEST
var lstTriangle = pascalTriangle(n);
 
 
// FORMAT OUTPUT AS WIKI TABLE
 
// [[a]] -> bool -> s -> s
function wikiTable(lstRows, blnHeaderRow, strStyle) {
return '{| class="wikitable" ' + (
strStyle ? 'style="' + strStyle + '"' : ''
) + lstRows.map(function (lstRow, iRow) {
var strDelim = ((blnHeaderRow && !iRow) ? '!' : '|');
 
return '\n|-\n' + strDelim + ' ' + lstRow.map(function (v) {
return typeof v === 'undefined' ? ' ' : v;
}).join(' ' + strDelim + strDelim + ' ');
}).join('') + '\n|}';
}
 
var lstLastLine = lstTriangle.slice(-1)[0],
lngBase = (lstLastLine.length * 2) - 1,
nWidth = lstLastLine.reduce(function (a, x) {
var d = x.toString().length;
return d > a ? d : a;
}, 1) * lngBase;
 
var lstTable = lstTriangle.map(function (lst) {
return lst.join(';;').split(';');
}).map(function (line, i) {
var lstPad = Array((lngBase - line.length) / 2);
return lstPad.concat(line).concat(lstPad);
});
 
return [
wikiTable(
lstTable,
false,
'text-align:center;width:' + nWidth + 'em;height:' + nWidth +
'em;table-layout:fixed;'
),
 
JSON.stringify(lstTriangle)
].join('\n\n');
})(7);</lang>
 
Output:
 
{| class="wikitable" style="text-align:center;width:26em;height:26em;table-layout:fixed;"
|-
| || || || || || || 1 || || || || || ||
|-
| || || || || || 1 || || 1 || || || || ||
|-
| || || || || 1 || || 2 || || 1 || || || ||
|-
| || || || 1 || || 3 || || 3 || || 1 || || ||
|-
| || || 1 || || 4 || || 6 || || 4 || || 1 || ||
|-
| || 1 || || 5 || || 10 || || 10 || || 5 || || 1 ||
|-
| 1 || || 6 || || 15 || || 20 || || 15 || || 6 || || 1
|}
 
<lang JavaScript>[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1],[1,5,10,10,5,1],[1,6,15,20,15,6,1]]</lang>
 
=={{header|jq}}==
9,659

edits