Advertisement
bai_onzi

Fill Matrix - B

Apr 8th, 2023
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let matrixSize = +gets();
  2.  
  3. let matrix = Array.from(Array(matrixSize), () => new Array(matrixSize).fill(0));
  4.  
  5. //col     0 1 2
  6. //row 0 - 1 6 7
  7. //row 1 - 2 5 8
  8. //row 2 - 3 4 9
  9.  
  10. let num = 1;
  11. for(let coll = 0; coll < matrixSize; coll++){
  12.     if(coll % 2 === 0){
  13.         for(let row = 0; row < matrixSize; row++){
  14.             matrix[row][coll] = num;
  15.             num++
  16.         }
  17.     }else{
  18.         for(let row = matrixSize-1; row >= 0; row--){
  19.             matrix[row][coll] = num;
  20.             num++
  21.         }
  22.     }
  23. }
  24.  
  25. for(let coll = 0; coll < matrixSize; coll++){
  26.     console.log(matrix[coll].join(' '));
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement