Skip to content

Instantly share code, notes, and snippets.

@22a
Created November 20, 2017 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 22a/002e5964ddc9866c75b7a3a88ee728a4 to your computer and use it in GitHub Desktop.
Save 22a/002e5964ddc9866c75b7a3a88ee728a4 to your computer and use it in GitHub Desktop.
Functional Katas, Token! 20-11-2017
const rows = 4
const columns = 4
const field = [
['*', '.', '.', '.'],
['.', '.', '.', '.'],
['.', '*', '.', '.'],
['.', '.', '.', '.']
]
const calcHint = (field, m, n, i, j) => {
if (field[i][j] === '*') {
return '*'
}
let total = 0
const startI = Math.max(0, i - 1)
const stopI = Math.min(m - 1, i + 1)
const startJ = Math.max(0, j - 1)
const stopJ = Math.min(n - 1, j + 1)
for (let ii = startI; ii <= stopI; ii++) {
for (let jj = startJ; jj <= stopJ; jj++) {
if (!(i === ii && j === jj) && field[ii][jj] === '*') {
total++
}
}
}
return total
}
const hintField = []
for (let i = 0; i < rows; i++) {
hintField.push([])
for (let j = 0; j < columns; j++) {
hintField[i].push(calcHint(field, rows, columns, i, j))
}
}
console.log(hintField)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment