Skip to content

Instantly share code, notes, and snippets.

@devNoiseConsulting
Last active March 16, 2017 16:20
Show Gist options
  • Save devNoiseConsulting/24c767482638fefa4c6ed8dc4b3d1bcc to your computer and use it in GitHub Desktop.
Save devNoiseConsulting/24c767482638fefa4c6ed8dc4b3d1bcc to your computer and use it in GitHub Desktop.
Magic Square Check - PhillyDev Slack #daily_programmer - 20170301
function magicSquareCheck(square) {
checkIndices = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];
return checkIndices.map(squareCheck).reduce(allPass, true);
function squareCheck(i) {
let currentCheck = new Array(square[i[0]], square[i[1]], square[i[2]]);
currentCheck = currentCheck.reduce(sumArray, 0);
return (currentCheck == 15) ? true : false;
}
function sumArray(a, b) {
return a + b;
}
function allPass(a, b) {
return a && b;
}
}
console.log(magicSquareCheck([8, 1, 6, 3, 5, 7, 4, 9, 2]));
console.log(magicSquareCheck([2, 7, 6, 9, 5, 1, 4, 3, 8]));
console.log(magicSquareCheck([3, 5, 7, 8, 1, 6, 4, 9, 2]));
console.log(magicSquareCheck([8, 1, 6, 7, 5, 3, 4, 9, 2]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment