JSFiddle - React, Tailwind, and code Playground

by Jessie Lau

JavaScript

function diagonalSum(matrix){
    var res = [];
    for(var i = 0; i < matrix.length; i++){
        var current = matrix[i];
        for(var j = 0; j < current.length; j++){
            res.push(current[j]);
            break;
        }
    }
    return res;
}

console.log(diagonalSum[[1, 2], [3, 4]]);
//console.log(diagonalSum([[1, 2, 3], [4, 5, 6], [7, 8, 9]]));
//console.log(diagonalSum([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]));