Skip to content

Instantly share code, notes, and snippets.

@Karnak19
Created October 6, 2020 10:28
Show Gist options
  • Save Karnak19/1c2e70b93127c6b849cd052f62e5a90c to your computer and use it in GitHub Desktop.
Save Karnak19/1c2e70b93127c6b849cd052f62e5a90c to your computer and use it in GitHub Desktop.
/* eslint-disable no-lonely-if */
function generateBoard(nbRow, nbCol) {
const board = []
for (let i = 0; i < nbRow; i++) {
const row = []
for (let j = 0; j < nbCol; j++) {
if (i % 2 === 0) {
if (j % 2 === 0) {
row.push('x')
} else {
row.push(' ')
}
} else {
if (j % 2 !== 0) {
row.push('X')
} else {
row.push(' ')
}
}
}
board.push(row)
}
return board
}
function betterGenerate(nbRow, nbCol) {
let board = new Array(nbRow).fill(new Array(nbCol).fill(''))
board = board.map((row, i) => {
return row.map((_, j) => {
if (i % 2 === 0) {
return j % 2 === 0 ? 'x' : ' '
} else {
return j % 2 === 0 ? ' ' : 'x'
}
})
})
return board
}
console.log('Easy')
console.table(generateBoard(4, 4))
console.log('---------------------')
console.log('Hard')
console.table(betterGenerate(4, 4))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment