Diagonal Difference

  • + 0 comments

    Oh nice, I did this too in Javascript! I was afraid it wouldn't work because the pattern "seemed" to be that the x/y coordinates added up to be 1 less than N.

    var backSlash = 0;
    var forwardSlash = 0;
    for (var i = 0; i < n; i++) {
        for (var j = 0; j < n; j++) {
            if (i == j) {
                backSlash += a[i][j];
            }
            if (i + j == n-1) {
                forwardSlash += a[i][j];
            }
        }
    }
    
    console.log(Math.abs(backSlash - forwardSlash));