Skip to content

Instantly share code, notes, and snippets.

@jshakes
Created August 2, 2017 14:10
Show Gist options
  • Save jshakes/7098ff0046bbc1de1a21f66aa51d5ec1 to your computer and use it in GitHub Desktop.
Save jshakes/7098ff0046bbc1de1a21f66aa51d5ec1 to your computer and use it in GitHub Desktop.
var Sudoku = function(data)
{
// Private methods
// -------------------------
function makeColArr() {
let columns = [];
let valid = true;
data.forEach((row, rowIndex) => {
columns.push([]);
row.forEach((cell, cellIndex) => {
const colRow = data[cellIndex];
const colCell = row[rowIndex];
if(!cell) {
valid = true;
return;
}
columns[rowIndex].push(cell);
});
});
return valid ? columns: null;
}
// Public methods
// -------------------------
return {
isValid: function() {
let rowsValid = true;
let columnsValid = true;
const columns = makeColArr();
if(!columns) {
return false;
}
data.forEach((row, rowIndex) => {
row.forEach((cell, cellIndex) => {
column = columns[cellIndex];
rowsValid = row && row.indexOf(cellIndex) > -1;
columnsValid = column && column.indexOf(cellIndex) > -1;
});
});
return rowsValid;
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment