How do I check for major diagonal conflicts in an array of arrays using
JavaScript?
For example, say I have this array of arrays:
Board = [
[ 0, 0, 0, 1],
[ 1, 0, 0, 1],
[ 0, 1, 0, 0]
[ 0, 0, 1, 0]
]
And now I want to check for main diagonal conflicts, such that no major
diagonal contains more than one 1. I've experimented with a variety of
loops, but can't seem to clinch the right code.
hi ,
ReplyDeletethis is a solution if we suppose n * n matrices
var hasMajorDiagonal =function(Board,c) {
var count = 0;
var i = 0;
if ( c < 0) {
i = c * -1;
c = 0;
}
for (; i < Board.length && c < Board.length ; i++ , c++) {
count += Board[i][c]
}
return count > 1;
}
var hasAnyMajorDiagonal = function(Board){
i = -1 * Board.length-1 ;
for (; i < Board.length; i++) {
if( hasMajorDiagonal(Board,i)){
return true ;
}
}
return false;
}