Created
November 20, 2017 22:23
-
-
Save 22a/002e5964ddc9866c75b7a3a88ee728a4 to your computer and use it in GitHub Desktop.
Functional Katas, Token! 20-11-2017
This file contains hidden or 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
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