Zhorzh

Untitled

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