I\'m working on a javascript sudoku, and as far as validation of input goes, I\'m stumped. As each value is entered into the sudoku table, I have an onkeyup triggering a functio
jsBin demo
var x = 0, map = [
[x, x, 4, 5, x, 3, x, 7, x],
[x, x, x, x, x, x, 3, 1, x],
[3, 5, x, x, x, 9, 2, x, x],
[x, x, x, x, 2, x, 9, 3, 7],
[6, x, 9, x, x, x, 4, x, 8],
[4, 7, 2, x, x, x, x, x, x],
[x, x, 1, x, x, x, x, 5, 2],
[x, 4, 5, x, x, x, x, x, x],
[x, 6, x, 8, x, 1, 7, x, x]
];
// This is what the user played
// after every user input update the "user" object.
var user = {};
function numberExists() {
var err = [];
for(var r=0; r<9; r++){ // iterate map rows
if(map[r][user.cell] === user.num){ // current row, specific cell == Match?
err.push(user.num +" exists in Row"+ r +" Cell"+ user.cell);
}
if(r === user.row){ // Is this row selected by the user?
for(var c=0; c<9; c++){ // Iterate all cells of this row
if(map[r][c] === user.num){ // Match?
err.push(user.num +" exists in Row"+ r + " Cell"+ c);
}
}
}
}
if(err.length){
console.log( err.join(" and ") );
} else { // No errors
console.log( "GOOD! Row"+ user.row +" Cell"+ user.cell );
}
}
// TEST /////
user.row = 4;
user.cell = 3;
user.num = 8;
numberExists(); // Trigger search!
will trigger (in the above example using r4, c3, n8)
"8 exists in Row4 Cell8 and 8 exists in Row8 Cell3"