It is required to create a two-dimensional array of 10x5 and fill it with numbers from 100 to 200, then output the result in the form of a table (rows-columns) and sort. I am experiencing difficulties with JS: I can’t fill the array, but the result is displayed in one line.

var arr = [ [], [], [], [], [] ]; for (var i = 0; i < 15; i++) { arr[i] = Math.floor(Math.random() * (200 - 100 + 1)) + 100; } console.log(arr[1]); 

  • 2
    SO is not for homework. - Alexey Ten
  • one
    I want to know what my mistake is - The Marafonskiy
  • 2
    In that you fill a one-dimensional array. To fill a two-dimensional array obviously there should be two cycles. - Alexey Ten
  • @AlexeyTen, not necessary - Grundy
  • @Grundy required. Of course, you can hide them for map / forEach, etc. but under the hood there will be two cycles. - Alexey Ten

4 answers 4

Check whether this is what you need - and then according to your description it is very difficult to understand the details of the task.

 var cmpr = function(a, b) { if (+a > +b) {return 1} else {return -1} } var arr=[]; for(var i=0;i<5;i++){ arr[i]=[]; for(var j=0;j<10;j++){ arr[i][j] = Math.floor(Math.random()*(200-100+1))+100 } arr[i].sort(cmpr) } console.log(arr); 

  • I would like to know, for example, we have a multidimensional array how to make arrays inside it appear in a column, like a matrix, for example - The Marafonskiy
  • @TheMarafonskiy where "were displayed"? - Igor
  • @TheMarafonskiy, console.log (JSON.stringify (arr, null, '\ t')); - yarkov_aleksei
  • @yarkov_aleksei, console.table - Grundy
  • @TheMarafonskiy, non-standard This feature is a non-standard track. Do not use it for production. There may also be a large incompatibilities between the changes and the behavior. - yarkov_aleksei

You can try using recursions -

 // m - matrix // x - horizontal size // y - vertical size const fillMatrix = ( matrix, horizontalSize, verticalSize, provider ) => { const horizontal = ( m, x, y ) => m.push( provider( m.length, y ) ) < x ? horizontal( m, x, y ) : m; const vertical = ( m, x, y ) => m.push( horizontal( [], x, m.length ) ) < y ? vertical( m, x, y ) : m; return vertical( matrix, horizontalSize, verticalSize ); } let matrix = fillMatrix( [], 3, 3, (x, y) => ({ x, y }) ) let string = matrix.reduce( ( vertical, item ) => { vertical += item.reduce( ( horizontal, { x, y } ) => { horizontal += `[x: ${ x }, y: ${ y }]`; return horizontal; }, ""); vertical += "\n"; return vertical; } , "" ); console.log( string ); 

     function getRand() { return Math.floor(Math.random() * (200 - 100 + 1)) + 100; } var arr = [ for (r of new Array(5))[ for (c of new Array(10)) getRand()] ]; console.log(arr); 

    Forgot about sorting

    var arr = arr.map(el => el.sort((a, b) => a - b));

       var arr = new Array(10); for (var i = 0; i < 10; i++) { arr[i] = new Array(5); } for (var i = 0; i < 10; i++) { for (var j = 0; j < 5; j++) { arr[i][j] = Math.floor(Math.random() * (200 - 100 + 1)) + 100; } } console.log(arr);