Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

diagonal Difference in 2D arrays javascript

function diagonalDifference(arr) {
    let leftToRight=[], rightToleft = []
    arr.forEach((ar, index)=>{
        leftToRight.push(ar[0+index])
        rightToleft.push(ar[ar.length - 1 - index])
    })
    const leftDiagnolSum = leftToRight.reduce((acc, val)=>acc+val)
    const rightDiagnolSum = rightToleft.reduce((acc, val)=>acc+val)
    return Math.abs(leftDiagnolSum - rightDiagnolSum)

}
 
PREVIOUS NEXT
Tagged: #diagonal #Difference #arrays #javascript
ADD COMMENT
Topic
Name
4+3 =