Multiplication tables: Difference between revisions

Content added Content deleted
Line 1,930:
 
=={{header|JavaScript}}==
 
===Imperative===
 
<lang html4strict><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<head>
Line 1,987 ⟶ 1,990:
 
 
===Functional (ES5)===
or, in a functional style of JavaScript, without iteration:
 
<lang JavaScript>// [1..12]Heading and table
// n --> n --> [n]s
(function range(m, n) {
// [m..n]
return Array.apply(
function cellrange(nm, wn) {
null, Array(n - m + 1)
return Array.apply(null, Array(n - m + 1)).map(
).map( function (x, i) {
return m + i;
});
);
}
 
// '[[a]] -> bool 1'-> ..s '-> 144's
function wikiTable(lstRows, blnHeaderRow, strStyle) {
// n --> n --> s
var css = strStyle ? 'style="' + strStyle + '"' : '';
function cell(n, w) {
return Array(w - n.toString().length + 1).join(' ') + n;
}
 
return '{| class="wikitable" ' + css + lstRows.map(
( function (m, nlstRow, colWidthiRow) {
var strDelim = ((blnHeaderRow && !iRow) ? '!' : '|'),
strDbl = strDelim + strDelim;
 
return '\n|-\n' + strDelim + ' ' + lst.slice(i)lstRow.map(function (xv) {
// Heading and table
return typeof v === 'undefined' ? ' ' : v;
// n --> n --> n --> s
}).join('\n ' + strDbl + ' ');
(function (m, n, colWidth) {
}).join('') + '\n|}';
}
 
// 1.. 12
var lstRange = range(m, n),
// x 1 2 3 4 5 6 7 8 9 10 11 12
// 5 space column widths
lstTable = [['x'].concat(lstRange)].concat(
pad = function (x) { return cell(x || ' ', colWidth) },
 
lstRange.map(function (iRow, i, lst) {
// x 1 2 3 4 5 6 7 8 9 10 11 12
lstTable = [['x'].concat(lstRange)].concat(
 
// );multiplier
lstRange.map(function (iRow, i, lst) {
return [iRow].concat(
 
// gap to left (triangle of //numbers multiplieronly)
Array.apply(null, return [iRow]Array(i)).concat(
 
// gap to left (triangle of numbers only)products
Arraylst.apply(null, Arrayslice(i)).concatmap(function (x) {
return x * iRow;
// products
lst.slice(i).map(function (x) {
return x * iRow;
})
)
);
})
})
);
})
);
 
// Formatted as WikiTable
return wikiTable(
lstTable, true,
'text-align:center;width:33em;height:33em;table-layout:fixed;'
)
 
})(1, 12, 5);</lang>
// Stringified table of padded lines
// [[s]] --> s
return lstTable.map(function (row) {
return row.map(pad).join('');
}).join('\n');
})(1, 12, 5);
</lang>
 
Output:
 
{| class="wikitable" style="text-align:center;width:33em;height:33em;table-layout:fixed;"
<pre> x 1 2 3 4 5 6 7 8 9 10 11 12
|-
1 1 2 3 4 5 6 7 8 9 10 11 12
! x !! 1 !! 2 !! 3 !! 4 !! 5 !! 6 !! 7 !! 8 !! 9 !! 10 !! 11 !! 12 14 16 18 20 22 24
|-
3 9 12 15 18 21 24 27 30 33 36
| 1 || 1 4|| 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9 || 10 16 20 24 28 32 36 40 44|| 11 || 4812
|-
5 25 30 35 40 45 50 55 60
| 2 || 6 || 4 || 6 || 8 || 10 || 12 || 14 || 16 || 18 || 20 || 22 || 36 42 48 54 60 66 7224
|-
7 49 56 63 70 77 84
| 3 || 8 || || 9 || 12 || 15 || 18 || 21 || 24 || 27 || 30 || 33 || 64 72 80 88 9636
|-
9 81 90 99 108
| 4 || 10 || || || 16 || 20 || 24 || 28 || 32 || 36 || 40 || 44 || 100 110 12048
|-
11 121 132
| 5 || 12 || || || || 25 || 30 || 35 || 40 || 45 || 50 || 55 || 144</pre>60
|-
| 6 || || || || || || 36 || 42 || 48 || 54 || 60 || 66 || 72
|-
| 7 || || || || || || || 49 || 56 || 63 || 70 || 77 || 84
|-
| 8 || || || || || || || || 64 || 72 || return80 x|| *88 iRow;|| 96
|-
| 9 || || || || || || || || || 81 || 90 || 99 || 108
|-
| 10 || || || || || || || || || || 100 || 110 || 120
|-
| 11 || || || || || || || || || || || 121 || 132
|-
| 12 || 1 || 1|| || 2 || 3 || 4|| || 5 || 6 || 7|| || 8 9 10 11 12144
|}
 
=={{header|Julia}}==