I'm making a chess board game and I'm new to JavaScript. I was just wondering if someone could look at my Javascript code and see how I can simplify and make it more optimized. I repetitively keep on writing the same code that I'm pretty sure could be simplified. It's where there is some comments that say, "This is where I display my dots."
Here's my JavaScript code:
//dragging white pawns
function allowDrop(event) {
event.preventDefault();
}
function drag(event) {
event.dataTransfer.setData("text", event.target.id);
}
function drop(event) {
event.preventDefault();
var data = event.dataTransfer.getData("text");
var newNode = document.getElementById(data).cloneNode(true);
// Clear the drag square of the piece IMG...
clearChildren( document.getElementById(data).parentNode);
clearChildren(event.target);
event.target.appendChild(newNode);
// Finally, remove the "empty_squares5" class from the drop square.
event.target.classList.remove( 'empty_squares5' );
}
function clearChildren(el) {
while (el.firstChild) {
el.removeChild(el.firstChild);
}
}
//This is for displaying dots
const dots = document.querySelectorAll(".empty_squares5 img");
const seventhSquares = document.querySelectorAll(".seventh_squares");
let arrayDots = Array.from(dots);
let arraySeventhSquares = Array.from(seventhSquares);
console.log(arrayDots);
console.log(arraySeventhSquares);
function showDots() {
arraySeventhSquares[0].style.backgroundColor = "#4682B4";
arrayDots[0].style.display= "inline";
arrayDots[8].style.display = "inline";
}
function dontShowDots(event) {
var x = event.buttons;
if (x == 1) {
arraySeventhSquares[1].style.backgroundColor = "lightgreen";
arraySeventhSquares[2].style.backgroundColor = "slategray";
arraySeventhSquares[3].style.backgroundColor = "lightgreen";
arraySeventhSquares[4].style.backgroundColor = "slategray";
arraySeventhSquares[5].style.backgroundColor = "lightgreen";
arraySeventhSquares[6].style.backgroundColor = "slategray";
arraySeventhSquares[7].style.backgroundColor = "lightgreen";
arrayDots[1].style.display = "none";
arrayDots[9].style.display = "none";
arrayDots[2].style.display = "none";
arrayDots[10].style.display = "none";
arrayDots[3].style.display = "none";
arrayDots[11].style.display = "none";
arrayDots[4].style.display = "none";
arrayDots[12].style.display = "none";
arrayDots[5].style.display = "none";
arrayDots[13].style.display = "none";
arrayDots[6].style.display = "none";
arrayDots[14].style.display = "none";
arrayDots[7].style.display = "none";
arrayDots[15].style.display = "none";
}
}
function showDots2() {
arraySeventhSquares[1].style.backgroundColor = "#4682B4";
arrayDots[1].style.display= "inline";
arrayDots[9].style.display = "inline";
}
function dontShowDots2(event) {
var x = event.buttons;
if (x == 1) {
arraySeventhSquares[0].style.backgroundColor = "slategray";
arraySeventhSquares[2].style.backgroundColor = "slategray";
arraySeventhSquares[3].style.backgroundColor = "lightgreen";
arraySeventhSquares[4].style.backgroundColor = "slategray";
arraySeventhSquares[5].style.backgroundColor = "lightgreen";
arraySeventhSquares[6].style.backgroundColor = "slategray";
arraySeventhSquares[7].style.backgroundColor = "lightgreen";
arrayDots[0].style.display = "none";
arrayDots[8].style.display = "none";
arrayDots[2].style.display = "none";
arrayDots[10].style.display = "none";
arrayDots[3].style.display = "none";
arrayDots[11].style.display = "none";
arrayDots[4].style.display = "none";
arrayDots[12].style.display = "none";
arrayDots[5].style.display = "none";
arrayDots[13].style.display = "none";
arrayDots[6].style.display = "none";
arrayDots[14].style.display = "none";
arrayDots[7].style.display = "none";
arrayDots[15].style.display = "none";
}
}
function showDots3() {
arraySeventhSquares[2].style.backgroundColor = "#4682B4";
arrayDots[2].style.display= "inline";
arrayDots[10].style.display = "inline";
}
function dontSD3(event) {
var x = event.buttons;
if (x == 1) {
arraySeventhSquares[0].style.backgroundColor = "slategray";
arraySeventhSquares[1].style.backgroundColor = "lightgreen";
arraySeventhSquares[3].style.backgroundColor = "lightgreen";
arraySeventhSquares[4].style.backgroundColor = "slategray";
arraySeventhSquares[5].style.backgroundColor = "lightgreen";
arraySeventhSquares[6].style.backgroundColor = "slategray";
arraySeventhSquares[7].style.backgroundColor = "lightgreen";
arrayDots[0].style.display = "none";
arrayDots[8].style.display = "none";
arrayDots[1].style.display = "none";
arrayDots[9].style.display = "none";
arrayDots[3].style.display = "none";
arrayDots[11].style.display = "none";
arrayDots[4].style.display = "none";
arrayDots[12].style.display = "none";
arrayDots[5].style.display = "none";
arrayDots[13].style.display = "none";
arrayDots[6].style.display = "none";
arrayDots[14].style.display = "none";
arrayDots[7].style.display = "none";
arrayDots[15].style.display = "none";
}
}
function showDots4() {
arraySeventhSquares[3].style.backgroundColor = "#4682B4";
arrayDots[3].style.display= "inline";
arrayDots[11].style.display = "inline";
}
function dontShowDots4(event) {
var x = event.buttons;
if (x == 1) {
arraySeventhSquares[0].style.backgroundColor = "slategray";
arraySeventhSquares[2].style.backgroundColor = "slategray";
arraySeventhSquares[1].style.backgroundColor = "lightgreen";
arraySeventhSquares[4].style.backgroundColor = "slategray";
arraySeventhSquares[5].style.backgroundColor = "lightgreen";
arraySeventhSquares[6].style.backgroundColor = "slategray";
arraySeventhSquares[7].style.backgroundColor = "lightgreen";
arrayDots[0].style.display = "none";
arrayDots[8].style.display = "none";
arrayDots[2].style.display = "none";
arrayDots[10].style.display = "none";
arrayDots[1].style.display = "none";
arrayDots[9].style.display = "none";
arrayDots[4].style.display = "none";
arrayDots[12].style.display = "none";
arrayDots[5].style.display = "none";
arrayDots[13].style.display = "none";
arrayDots[6].style.display = "none";
arrayDots[14].style.display = "none";
arrayDots[7].style.display = "none";
arrayDots[15].style.display = "none";
}
}
function showDots5() {
arraySeventhSquares[4].style.backgroundColor = "#4682B4";
arrayDots[4].style.display= "inline";
arrayDots[12].style.display = "inline";
}
function dontShowDots5(event) {
var x = event.buttons;
if (x == 1) {
arraySeventhSquares[0].style.backgroundColor = "slategray";
arraySeventhSquares[1].style.backgroundColor = "lightgreen";
arraySeventhSquares[2].style.backgroundColor = "slategray";
arraySeventhSquares[3].style.backgroundColor = "lightgreen";
arraySeventhSquares[5].style.backgroundColor = "lightgreen";
arraySeventhSquares[6].style.backgroundColor = "slategray";
arraySeventhSquares[7].style.backgroundColor = "lightgreen";
arrayDots[0].style.display = "none";
arrayDots[8].style.display = "none";
arrayDots[1].style.display = "none";
arrayDots[9].style.display = "none";
arrayDots[2].style.display = "none";
arrayDots[10].style.display = "none";
arrayDots[3].style.display = "none";
arrayDots[11].style.display = "none";
arrayDots[5].style.display = "none";
arrayDots[13].style.display = "none";
arrayDots[6].style.display = "none";
arrayDots[14].style.display = "none";
arrayDots[7].style.display = "none";
arrayDots[15].style.display = "none";
}
}
function showDots6() {
arraySeventhSquares[5].style.backgroundColor = "#4682B4";
arrayDots[5].style.display= "inline";
arrayDots[13].style.display = "inline";
}
function dontShowDots6(event) {
var x = event.buttons;
if (x == 1) {
arraySeventhSquares[0].style.backgroundColor = "slategray";
arraySeventhSquares[1].style.backgroundColor = "lightgreen";
arraySeventhSquares[2].style.backgroundColor = "slategray";
arraySeventhSquares[3].style.backgroundColor = "lightgreen";
arraySeventhSquares[4].style.backgroundColor = "slategray";
arraySeventhSquares[6].style.backgroundColor = "slategray";
arraySeventhSquares[7].style.backgroundColor = "lightgreen";
arrayDots[0].style.display = "none";
arrayDots[8].style.display = "none";
arrayDots[1].style.display = "none";
arrayDots[9].style.display = "none";
arrayDots[2].style.display = "none";
arrayDots[10].style.display = "none";
arrayDots[3].style.display = "none";
arrayDots[11].style.display = "none";
arrayDots[4].style.display = "none";
arrayDots[12].style.display = "none";
arrayDots[6].style.display = "none";
arrayDots[14].style.display = "none";
arrayDots[7].style.display = "none";
arrayDots[15].style.display = "none";
}
}
function showDots7() {
arraySeventhSquares[6].style.backgroundColor = "#4682B4";
arrayDots[6].style.display= "inline";
arrayDots[14].style.display = "inline";
}
function dontShowDots7(event) {
var x = event.buttons;
if (x == 1) {
arraySeventhSquares[0].style.backgroundColor = "slategray";
arraySeventhSquares[1].style.backgroundColor = "lightgreen";
arraySeventhSquares[2].style.backgroundColor = "slategray";
arraySeventhSquares[3].style.backgroundColor = "lightgreen";
arraySeventhSquares[4].style.backgroundColor = "slategray";
arraySeventhSquares[5].style.backgroundColor = "lightgreen";
arraySeventhSquares[7].style.backgroundColor = "lightgreen";
arrayDots[0].style.display = "none";
arrayDots[8].style.display = "none";
arrayDots[1].style.display = "none";
arrayDots[9].style.display = "none";
arrayDots[2].style.display = "none";
arrayDots[10].style.display = "none";
arrayDots[3].style.display = "none";
arrayDots[11].style.display = "none";
arrayDots[4].style.display = "none";
arrayDots[12].style.display = "none";
arrayDots[5].style.display = "none";
arrayDots[13].style.display = "none";
arrayDots[7].style.display = "none";
arrayDots[15].style.display = "none";
}
}
function showDots8() {
arraySeventhSquares[7].style.backgroundColor = "#4682B4";
arrayDots[7].style.display= "inline";
arrayDots[15].style.display = "inline";
}
function dontShowDots8(event) {
var x = event.buttons;
if (x == 1) {
arraySeventhSquares[0].style.backgroundColor = "slategray";
arraySeventhSquares[1].style.backgroundColor = "lightgreen";
arraySeventhSquares[2].style.backgroundColor = "slategray";
arraySeventhSquares[3].style.backgroundColor = "lightgreen";
arraySeventhSquares[4].style.backgroundColor = "slategray";
arraySeventhSquares[5].style.backgroundColor = "lightgreen";
arraySeventhSquares[6].style.backgroundColor = "slategray";
arrayDots[0].style.display = "none";
arrayDots[8].style.display = "none";
arrayDots[1].style.display = "none";
arrayDots[9].style.display = "none";
arrayDots[2].style.display = "none";
arrayDots[10].style.display = "none";
arrayDots[3].style.display = "none";
arrayDots[11].style.display = "none";
arrayDots[4].style.display = "none";
arrayDots[12].style.display = "none";
arrayDots[5].style.display = "none";
arrayDots[13].style.display = "none";
arrayDots[6].style.display = "none";
arrayDots[14].style.display = "none";
}
}
html code:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="board">
<!-- 1st row -->
<div><img src="images/black_rook.png" draggable="true" ondragstart="drag(event)" height="65px" width="65px"></div>
<div><img src="images/black_knight.png" draggable="true" ondragstart="drag(event)" height="65px" width="65px"></div>
<div><img src="images/black_bishop.png" draggable="true" ondragstart="drag(event)" height="65px" width="65px"></div>
<div><img src="images/black_queen.png" draggable="true" ondragstart="drag(event)" height="65px" width="65px"></div>
<div><img src="images/black_king.png" draggable="true" ondragstart="drag(event)" height="65px" width="65px"></div>
<div><img src="images/black_bishop.png" draggable="true" ondragstart="drag(event)" height="65px" width="65px"></div>
<div><img src="images/black_knight.png" draggable="true" ondragstart="drag(event)"height="65px" width="65px"></div>
<div><img src="images/black_rook.png" draggable="true" ondragstart="drag(event)" height="65px" width="65px"></div>
<!-- 2nd row-->
<div><img src="images/black_pawn.png" draggable="true" ondragstart="drag(event)" class="black_pawns" height="65px" width="65px"></div>
<div><img src="images/black_pawn.png" draggable="true" ondragstart="drag(event)" class="black_pawns" height="65px" width="65px"></div>
<div><img src="images/black_pawn.png" draggable="true" ondragstart="drag(event)" class="black_pawns" height="65px" width="65px"></div>
<div><img src="images/black_pawn.png" draggable="true" ondragstart="drag(event)" class="black_pawns" height="65px" width="65px"></div>
<div><img src="images/black_pawn.png" draggable="true" ondragstart="drag(event)" class="black_pawns" height="65px" width="65px"></div>
<div><img src="images/black_pawn.png" draggable="true" ondragstart="drag(event)" class="black_pawns" height="65px" width="65px"></div>
<div><img src="images/black_pawn.png" draggable="true" ondragstart="drag(event)" class="black_pawns" height="65px" width="65px"></div>
<div><img src="images/black_pawn.png" draggable="true" ondragstart="drag(event)" class="black_pawns" height="65px" width="65px"></div>
<!-- 3rd row empty-->
<div class="empty_squares"></div>
<div class="empty_squares"></div>
<div class="empty_squares"></div>
<div class="empty_squares"></div>
<div class="empty_squares"></div>
<div class="empty_squares"></div>
<div class="empty_squares"></div>
<div class="empty_squares"></div>
<!-- 4th row empty-->
<div class="empty_squares"></div>
<div class="empty_squares"></div>
<div class="empty_squares"></div>
<div class="empty_squares"></div>
<div class="empty_squares"></div>
<div class="empty_squares"></div>
<div class="empty_squares"></div>
<div class="empty_squares"></div>
<!-- 5th row empty-->
<div class="empty_squares5" ondrop="drop(event)" ondragover="allowDrop(event)"><img src="images/greenDot.png" height="40px" width="60px"></div>
<div class="empty_squares5" ondrop="drop(event)" ondragover="allowDrop(event)"><img src="images/greenDot.png" height="40px" width="60px"></div>
<div class="empty_squares5" ondrop="drop(event)" ondragover="allowDrop(event)"><img src="images/greenDot.png" height="40px" width="60px"></div>
<div class="empty_squares5" ondrop="drop(event)" ondragover="allowDrop(event)"><img src="images/greenDot.png" height="40px" width="60px"></div>
<div class="empty_squares5" ondrop="drop(event)" ondragover="allowDrop(event)"><img src="images/greenDot.png" height="40px" width="60px"></div>
<div class="empty_squares5" ondrop="drop(event)" ondragover="allowDrop(event)"><img src="images/greenDot.png" height="40px" width="60px"></div>
<div class="empty_squares5" ondrop="drop(event)" ondragover="allowDrop(event)"><img src="images/greenDot.png" height="40px" width="60px"></div>
<div class="empty_squares5" ondrop="drop(event)" ondragover="allowDrop(event)"><img src="images/greenDot.png" height="40px" width="60px"></div>
<!-- 6th row empty-->
<div class="empty_squares5" ondrop="drop(event)" ondragover="allowDrop(event)"><img src="images/greenDot.png" height="40px" width="60px"></div>
<div class="empty_squares5" ondrop="drop(event)" ondragover="allowDrop(event)"><img src="images/greenDot.png" height="40px" width="60px"></div>
<div class="empty_squares5" ondrop="drop(event)" ondragover="allowDrop(event)"><img src="images/greenDot.png" height="40px" width="60px"></div>
<div class="empty_squares5" ondrop="drop(event)" ondragover="allowDrop(event)"><img src="images/greenDot.png" height="40px" width="60px"></div>
<div class="empty_squares5" ondrop="drop(event)" ondragover="allowDrop(event)"><img src="images/greenDot.png" height="40px" width="60px"></div>
<div class="empty_squares5" ondrop="drop(event)" ondragover="allowDrop(event)"><img src="images/greenDot.png" height="40px" width="60px"></div>
<div class="empty_squares5" ondrop="drop(event)" ondragover="allowDrop(event)"><img src="images/greenDot.png" height="40px" width="60px"></div>
<div class="empty_squares5" ondrop="drop(event)" ondragover="allowDrop(event)"><img src="images/greenDot.png" height="40px" width="60px"></div>
<!-- 7th row-->
<div class="seventh_squares"><img src="images/white_pawn.png" draggable="true" id="item1" ondragstart="drag(event)" onmousedown="dontShowDots(event)" onclick="showDots()" height="65px" width="65px"></div>
<div class="seventh_squares"><img src="images/white_pawn.png" draggable="true" id="item2" onmousedown="dontShowDots2(event)" onclick="showDots2()" height="65px" width="65px"></div>
<div class="seventh_squares"><img src="images/white_pawn.png" draggable="true" id="item3" onmousedown="dontSD3(event)" onclick="showDots3()" height="65px" width="65px"></div>
<div class="seventh_squares"><img src="images/white_pawn.png" draggable="true" id="item4" onmousedown="dontShowDots4(event)" onclick="showDots4()" height="65px" width="65px"></div>
<div class="seventh_squares"><img src="images/white_pawn.png" draggable="true" id="item5" onmousedown="dontShowDots5(event)" onclick="showDots5()" height="65px" width="65px"></div>
<div class="seventh_squares"><img src="images/white_pawn.png" draggable="true" id="item6" onmousedown="dontShowDots6(event)" onclick="showDots6()" height="65px" width="65px"></div>
<div class="seventh_squares"><img src="images/white_pawn.png" draggable="true" id="item7" onmousedown="dontShowDots7(event)" onclick="showDots7()" height="65px" width="65px"></div>
<div class="seventh_squares"><img src="images/white_pawn.png" draggable="true" id="item8" onmousedown="dontShowDots8(event)" onclick="showDots8()" height="65px" width="65px"></div>
<!-- 8th row-->
<div><img src="images/white_rook.png" draggable="true" ondragstart="drag(event)" height="65px" width="65px"></div>
<div><img src="images/white_knight.png" draggable="true" ondragstart="drag(event)" height="65px" width="65px"></div>
<div><img src="images/white_bishop.png" draggable="true" ondragstart="drag(event)" height="65px" width="65px"></div>
<div><img src="images/white_queen.png" draggable="true" ondragstart="drag(event)" height="65px" width="65px"></div>
<div><img src="images/white king.png" draggable="true" ondragstart="drag(event)" height="65px" width="65px"></div>
<div><img src="images/white_bishop.png" draggable="true" ondragstart="drag(event)" height="65px" width="65px"></div>
<div><img src="images/white_knight.png" draggable="true" ondragstart="drag(event)" height="65px" width="65px"></div>
<div><img src="images/white_rook.png" draggable="true" ondragstart="drag(event)" height="65px" width="65px"></div>
<script src="interactive.js"></script>
</div>
</body>
</html>
arrayDots[i]and sets style=display none only on those that are appropriate in each main function. You could for example use consts that keep an array of numbers representing each group of numbers you need to use asi's in each arrayDot[i] to get to the correct dot. This way you'll be able to reuse some code and just change the numbers you use to select each dot. You could also use if/else/switch logic if there is any particular logic to which dots you select.