Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export default function (data) {
- // your code starts here
- const newMatrix = []
- const result = data.reduce((acc,currRow,rowIndex)=>{
- acc = currRow.map((currNum,colIndex)=>{
- let sum = 0
- if ((data[rowIndex-1] )!== undefined){
- sum+=data[rowIndex-1][colIndex]
- } else {
- sum += 0
- }
- if ((data[rowIndex +1])!==undefined){
- sum += data[rowIndex +1][colIndex]
- } else {
- sum += 0
- }
- if ((data[colIndex -1])!==undefined){
- sum-= data[rowIndex][colIndex -1]
- } else {
- sum += 0
- }
- if ((data[colIndex+1])!==undefined){
- sum-=data[rowIndex][colIndex+1]
- } else {
- sum += 0
- }
- currNum = sum
- return currNum
- })
- newMatrix.push(acc)
- return newMatrix
- },[])
- return result
- // your code ends here
- }
Advertisement
Add Comment
Please, Sign In to add comment