Tiny array puzzle in a quirky page
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Spot the Bug: Array Mini</title>
<style>
body { font-family: sans-serif; padding: 1rem; }
#list { list-style: none; padding: 0; }
#list li { padding: .25rem 0; }
</style>
</head>
<body>
<h1>Array Oddity</h1>
<p>See the numbers below; the first two are swapped on purpose.</p>
<ul id="list"></ul>
<script>
// deliberate bug: the array is built but one value is miscalculated
const nums = [1, 2, 3, 4, 5];
const displayed = nums.map((n, i) => {
// swap first two elements by mistake when i == 0
if (i === 0) return nums[1];
if (i === 1) return nums[0];
return n;
});
const ul = document.getElementById('list');
displayed.forEach(v => {
const li = document.createElement('li');
li.textContent = v;
ul.appendChild(li);
});
</script>
</body>
</html>
Reply with what is broken and how you would fix it.