0+0 0+1 0+2 ... 0 1 2 3 4 5 6 7 1+0 1+1 1+2 ... => 1 2 3 4 5 6 7 8 ... 7+0 7+1 7+2 ... 7 8 9 10 11 12
'use strict' const SIZE = 8 let row,col; let text=""; for(row = 0; row < SIZE; row ++) { for (col = 0; col < SIZE; col ++) { if ((row+col)%2 == 0) { text += "_"; } else { text += "#"; } } text += "\n"; } console.log(text);
<div id="outputSpace"> nothing yet</div>
document.getElementById("outputSpace")
document.getElementById("outputSpace").innerText = text;
'use strict' const SIZE = 8 let row,col; let text=""; for(row = 0; row < SIZE; row ++) { for (col = 0; col < SIZE; col ++) { if ((row+col)%2 == 0) { text += "_"; } else { text += "#"; } } text += "\n"; } text += "\n"; document.getElementById("outputSpace").innerText = text;
<canvas id="chessboard" width="400", height="400", style="border:1px solid #000000;"> </canvas>
let canvas = document.getElementById("chessboard");
let ctx = canvas.getContext('2d'); let width = canvas.width; let height = canvas.height;
let boardSize = Math.min(width, height); let squareSize = boardSize/SIZE;
ctx.fillRect(col*squareSize, row*squareSize,squareSize, squareSize);
if ((row+col)%2 == 0) { ctx.fillStyle = "red"; } else { ctx.fillStyle = "black"; }
let canvas = document.getElementById("chessboard"); let ctx = canvas.getContext('2d'); let width = canvas.width; let height = canvas.height; let boardSize = Math.min(width, height); let squareSize = boardSize/SIZE; for(row = 0; row < SIZE; row ++) { for (col = 0; col < SIZE; col ++) { if ((row+col)%2 == 0) { ctx.fillStyle = "red"; } else { ctx.fillStyle = "black"; } ctx.fillRect(col*squareSize, row*squareSize,squareSize, squareSize); } }