JSFiddle - React, Tailwind, and code Playground
by joelpurra
HTML
<h1>
Array length test
</h1>
<p>
Attempts to set values (sparsley) in an array.
</p>
<p>
<code>arr[2<sup>n</sup>-1] = 2<sup>n</sup>-1</code> for <code>n=1..128</code>.
</p>
<h2>
Values
</h2>
<table>
<thead>
<tr>
<th scope="col">
n
</th>
<th scope="col">
2<sup>n</sup>-1
</th>
<th scope="col">
arr[2<sup>n</sup>-1]
</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
CSS
th,
td
{
padding: 3px;
font-family: monospace, sans-serif;
}
.mismatch{
color: red;
}
JavaScript
$(function() {
var arr = [],
i, power, index, $tableBody = $("tbody");
function getCell(val) {
var $td = $("<td />");
$td.html(val);
return $td;
}
function addRow(i, index, arrValue) {
var $tr = $("<tr />");
getCell(i).appendTo($tr);
getCell(index).appendTo($tr);
getCell(arrValue).appendTo($tr);
if(index !== arrValue)
{
$tr.addClass("mismatch");
}
$tr.appendTo($tableBody);
}
for (i = 1; i <= 128; i++) {
power = Math.pow(2, i);
index = power - 1;
arr[index] = index;
addRow(i, index, arr[index]);
}
});