Zhorzh

Untitled

Feb 2nd, 2022
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. const newMatrix = []
  2. const result = data.reduce((acc,currRow,rowIndex)=>{
  3. acc = currRow.map((currNum,colIndex)=>{
  4. let sum = 0
  5. if ((data[rowIndex-1] )!== undefined){
  6. sum+=data[rowIndex-1][colIndex]
  7. } else {
  8. sum += 0
  9. }
  10. if ((data[rowIndex +1])!==undefined){
  11. sum += data[rowIndex +1][colIndex]
  12. } else {
  13. sum += 0
  14. }
  15. if ((data[rowIndex][colIndex -1])!==undefined){
  16. sum-= data[rowIndex][colIndex -1]
  17. } else {
  18. sum += 0
  19. }
  20. if ((data[rowIndex][colIndex+1])!==undefined){
  21. sum-=data[rowIndex][colIndex+1]
  22. } else {
  23. sum += 0
  24. }
  25. currNum = sum
  26. return currNum
  27. })
  28. newMatrix.push(acc)
  29. return newMatrix
  30. },[])
  31. return result
Advertisement
Add Comment
Please, Sign In to add comment