Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //------------------------------------------
- // Write a function to print a chessboard of size n X n. See the example for more information.
- // The input comes as a single number argument n.
- // The output should be returned as a result of your function in the form of a string.
- //------------------------------------------
- function chessBoard(n) {
- let size = n;
- let board = "<div class=\"chessboard\">\n";
- for (let y = 0; y < size; y++) {
- board += " <div>\n"
- for (let x = 0; x < size; x++) {
- if ((x + y) % 2 == 0)
- board += " <span class=\"black\"></span>\n";
- else
- board += " <span class=\"white\"></span>\n";
- }
- board += " </div>\n";
- }
- board += "</div>";
- console.log(board);
- }
- chessBoard(3)
Advertisement
Add Comment
Please, Sign In to add comment