The Lockers Problem V2

by Graham Walters

HTML

<span id="total"></span>

<ul id="list"></ul>

CSS

body {
    font-family: "Inconsolata", "Monaco", "Andale Mono", "Lucida Console", "Bitstream Vera Sans Mono", "Courier New", Courier, monospace;
    line-height: 17px;
    font-size: 12px;
    color: #bf616a;
}

ul {
    list-style: none;
    padding: 0;
}

JavaScript

var list = document.getElementById('list');
var total = document.getElementById('total');

console.time('v2');
var openLockers = [];
for (var locker=1; locker<1000; locker++) {
    if (Number.isInteger( Math.sqrt(locker) )) {
        openLockers.push(locker);
    }
}
console.timeEnd('v2');

// List which lockers are still open
for (var l=0; l<openLockers.length; l++) {
    var li = document.createElement('li');
    li.innerHTML = 'Locker '+openLockers[l]+' is open';
    list.appendChild(li);
}

total.innerHTML = openLockers.length + ' Lockers are open';