/grid.js Secret
Created
April 28, 2024 22:12
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://eloquentjavascript.net/02_program_structure.html | |
// keyword: chessboard | |
// quiz at the end | |
const generate = (size = 0) => { | |
if (size === 0) { | |
return ''; | |
} | |
if (size === 1) { | |
return '#'; | |
} | |
let grid = ''; | |
for (let i = 1; i <= size; i++) { | |
for (let j = 1; j <= size + 1; j++) { | |
if (j === size + 1) { | |
grid += '\n'; | |
continue; | |
} | |
if (i % 2 !== 0) { | |
if (j % 2 !== 0) { | |
grid += ' '; | |
} else { | |
grid += '#'; | |
} | |
} else { | |
if (j % 2 !== 0) { | |
grid += '#'; | |
} else { | |
grid += ' '; | |
} | |
} | |
} | |
} | |
return grid; | |
} | |
const grid = generate(10); | |
console.log(grid); |
Author
bearzk
commented
Apr 28, 2024
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment