Filling up a 2D array with random numbers in javascript - Javascript Array Operation

Javascript examples for Array Operation:Multi Dimensional Array

Description

Filling up a 2D array with random numbers in javascript

Demo Code

ResultView the demo in separate window

<html>
   <head></head>
   <body> 
      <script>
debugger;/*from   w w  w.j  av a  2  s.com*/
var ground = generateGround(10, 10);
function generateGround(height, width)
{
  var ground = [];
  for (var y = 0 ; y < height; y++)
  {
    ground[y] = [];
    for (var x = 0; x < width; x++)
    {
        ground[y][x] = my();
    }
  }
  return ground;
  function my()
  {
    return (Math.random() * 5 | 0) + 6;
  }
}

      </script>  
   </body>
</html>

Related Tutorials