Skip to content

Instantly share code, notes, and snippets.

@KamMif
Created December 17, 2016 07:57
Show Gist options
  • Save KamMif/4404d4a3d7bdfe7058fa8ff8500501c8 to your computer and use it in GitHub Desktop.
Save KamMif/4404d4a3d7bdfe7058fa8ff8500501c8 to your computer and use it in GitHub Desktop.
var matrixExample = [
[1, 2, 3, 4],
[4, 5, 6, 5],
[7, 8, 9, 7],
[7, 8, 9, 7]
]
function sumMatrix(matrix) {
var arr = []
var arr2 = []
matrix.forEach(function(item, i){
if (i == 0) {
arr.push(item[0])
arr2.push(item[3])
}
else if (i == 1) {
arr.push(item[1])
arr2.push(item[2])
}
else if (i == 2) {
arr.push(item[2])
arr2.push(item[1])
}
else {
arr.push(item[3])
arr2.push(item[0])
}
})
var general_d = arr.reduce(function(sum, current) {
return sum + current;
}, 0)
var not_general_d = arr2.reduce(function(sum, current) {
return sum + current
}, 0)
var sum = general_d + not_general_d
console.log(sum)
}
sumMatrix(matrixExample);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment