Multiplication tables: Difference between revisions
Content added Content deleted
m (→lint) |
|||
Line 1,930: | Line 1,930: | ||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
===Imperative=== |
|||
<lang html4strict><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> |
<lang html4strict><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> |
||
<head> |
<head> |
||
Line 1,987: | Line 1,990: | ||
===Functional (ES5)=== |
|||
or, in a functional style of JavaScript, without iteration: |
|||
<lang JavaScript>// |
<lang JavaScript>// Heading and table |
||
// n --> n --> |
// n --> n --> s |
||
function |
(function (m, n) { |
||
// [m..n] |
|||
⚫ | |||
⚫ | |||
null, Array(n - m + 1) |
|||
⚫ | |||
function (x, i) { |
|||
return m + i; |
return m + i; |
||
} |
} |
||
⚫ | |||
⚫ | |||
} |
|||
// |
// [[a]] -> bool -> s -> s |
||
function wikiTable(lstRows, blnHeaderRow, strStyle) { |
|||
// n --> n --> s |
|||
var css = strStyle ? 'style="' + strStyle + '"' : ''; |
|||
⚫ | |||
return Array(w - n.toString().length + 1).join(' ') + n; |
|||
} |
|||
return '{| class="wikitable" ' + css + lstRows.map( |
|||
⚫ | |||
var strDelim = ((blnHeaderRow && !iRow) ? '!' : '|'), |
|||
strDbl = strDelim + strDelim; |
|||
⚫ | |||
// Heading and table |
|||
return typeof v === 'undefined' ? ' ' : v; |
|||
// n --> n --> n --> s |
|||
⚫ | |||
⚫ | |||
}).join('') + '\n|}'; |
|||
} |
|||
// 1.. 12 |
|||
var lstRange = range(m, n), |
|||
⚫ | |||
// 5 space column widths |
|||
⚫ | |||
pad = function (x) { return cell(x || ' ', colWidth) }, |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
return [iRow].concat( |
|||
// gap to left (triangle of numbers only) |
|||
Array.apply(null, Array(i)).concat( |
|||
// products |
|||
lst.slice(i).map(function (x) { |
|||
return x * iRow; |
|||
// products |
|||
⚫ | |||
⚫ | |||
⚫ | |||
) |
|||
⚫ | |||
}) |
}) |
||
⚫ | |||
); |
); |
||
}) |
|||
); |
|||
// Formatted as WikiTable |
|||
return wikiTable( |
|||
lstTable, true, |
|||
'text-align:center;width:33em;height:33em;table-layout:fixed;' |
|||
) |
|||
⚫ | |||
// Stringified table of padded lines |
|||
// [[s]] --> s |
|||
return lstTable.map(function (row) { |
|||
return row.map(pad).join(''); |
|||
⚫ | |||
⚫ | |||
⚫ | |||
</lang> |
|||
Output: |
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 |
|||
|- |
|||
⚫ | |||
2 |
! x !! 1 !! 2 !! 3 !! 4 !! 5 !! 6 !! 7 !! 8 !! 9 !! 10 !! 11 !! 12 |
||
|- |
|||
3 9 12 15 18 21 24 27 30 33 36 |
|||
| 1 || 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9 || 10 || 11 || 12 |
|||
|- |
|||
5 25 30 35 40 45 50 55 60 |
|||
| 2 || || 4 || 6 || 8 || 10 || 12 || 14 || 16 || 18 || 20 || 22 || 24 |
|||
|- |
|||
⚫ | |||
| 3 || || || 9 || 12 || 15 || 18 || 21 || 24 || 27 || 30 || 33 || 36 |
|||
|- |
|||
⚫ | |||
| 4 || || || || 16 || 20 || 24 || 28 || 32 || 36 || 40 || 44 || 48 |
|||
|- |
|||
⚫ | |||
| 5 || || || || || 25 || 30 || 35 || 40 || 45 || 50 || 55 || 60 |
|||
|- |
|||
| 6 || || || || || || 36 || 42 || 48 || 54 || 60 || 66 || 72 |
|||
|- |
|||
⚫ | |||
|- |
|||
⚫ | |||
|- |
|||
⚫ | |||
|- |
|||
| 10 || || || || || || || || || || 100 || 110 || 120 |
|||
|- |
|||
⚫ | |||
|- |
|||
⚫ | |||
⚫ | |||
=={{header|Julia}}== |
=={{header|Julia}}== |