I recently crafted a binary word search puzzle using HTML, CSS and JavaScript to dynamically build grids within a grid:
var board = document.getElementById('board');
var characters = [
'H','U','O','M','S','F','I','R','E','I',
'P','O','B','P','O','S','P','K','K','A',
'S','E','P','T','E','M','B','E','R','F',
'Y','T','A','E','W','D','T','I','O','T',
'W','W','N','S','R','A','I','E','B','E',
'X','E','H','P','T','A','Y','Z','S','R',
'S','L','X','G','Y','E','T','U','F','N',
'K','F','D','R','E','Q','R','I','X','O',
'Y','T','H','Q','Z','H','X','N','O','O',
'L','H','Z','D','F','O','U','R','T','N'
];
for (var iCharacters = 0; iCharacters < characters.length; iCharacters++) {
var binary = '0' + characters[iCharacters].charCodeAt(0).toString(2);
var square = '<div class="square">';
for (var iBinary = 0; iBinary < binary.length; iBinary++) {
if (iBinary == 4) {
square += '<div class="empty"></div>';
square += '<div>' + binary.charAt(iBinary) + '</div>';
}
else
square += '<div>' + binary.charAt(iBinary) + '</div>';
}
board.innerHTML += square + '</div>';
}
html, body {
background: linear-gradient(90deg, #00C9FF 0%, #92FE9D 100%);
}
.board {
width: 800px;
height: 800px;
display: grid;
grid-template-columns: repeat(10, 10%);
grid-template-rows: repeat(10, 10%);
grid-gap: 5px;
border-radius: 5px;
}
.square {
display: grid;
grid-template-columns: repeat(3, 33%);
grid-template-rows: repeat(3, 33%);
border-radius: 5px;
background-color: #fff7;
}
.square div {
display: flex;
align-content: center;
justify-content: center;
font-size: 15px;
color: #444;
}
.square div:nth-child(1) { border-top-left-radius: 5px; }
.square div:nth-child(3) { border-top-right-radius: 5px; }
.square div:nth-child(7) { border-bottom-left-radius: 5px; }
.square div:nth-child(9) { border-bottom-right-radius: 5px; }
.square .empty {
background-color: #0003;
border-radius:5px;
}
<div id="board" class="board"></div>
Since I don't use HTML, CSS and JavaScript as often as I used to, it would be nice to have someone review my code to answer the following questions:
- Is my code legible?
- Are there more efficient methods I could have used?
Note: The use of Hungarian notation isn't something I typically do, however in the case where I have multiple iteration variables, I utilize the notation to help denote that the variable I'm working with is, an iteration variable. As such iBinary
does not mean that it is an integer, but rather an iteration variable. To me, this is easier to follow when it doesn't make sense to use x,y,z
or i,j,k
.