0

All rows always have same numbers, why? They should take random number to populate it. Also, how can I repair it? Once I saw answer here but now I cannot find it.

var mapSizex = 5;
var mapSizey = 6;
var mapArray = [];

$(function() {
  console.log("ready!");
  $('#map-draw').html(drawMap());
});

function mapGenerator() {
  for (i = 0; i < mapSizex; i++) {
    for (x = 0; x < mapSizey; x++) {
      mapArray[i, x] = getRandom(1, 5);
    }
  }
}

function drawMap() {
  mapGenerator();
  var map = '';
  tileID = 0;
  for (i = 0; i < mapSizex; i++) {
    map = map + '<br style="clear: both;">';
    for (x = 0; x < mapSizey; x++) {
      map = map + '<div class="tile tileID' + tileID + '">' + mapArray[i, x] + '</div>';
      tileID++;
    }
  }
  return map;
}

function getRandom(min, max) {
  var x = Math.floor((Math.random() * max) + min);
  return x;
}
.tile {
  float: left;
  height: 20px;
  width: 20px;
  border: 1px solid black;
  text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="main-container">
  <div id="map-container">
    <div id="map-draw"></div>
  </div>
</div>

To post it I need to add some more content but all included information's should be enough to find what I mean.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
BatOOn
  • 131
  • 8
  • 4
    Dimensional arrays don´t work like that. mapArray[i,x] is invalid, it should be mapArray[i][x] – juvian Oct 31 '16 at 16:31
  • Possible duplicate of [How can I create a two dimensional array in JavaScript?](http://stackoverflow.com/questions/966225/how-can-i-create-a-two-dimensional-array-in-javascript) – juvian Oct 31 '16 at 16:32
  • 3
    As an additional note, your syntax won't throw an error because it is valid use of the [comma operator](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Comma_Operator), which explains your results. – ASDFGerte Oct 31 '16 at 16:33
  • [][] --> Gooood right :D sorry for stupid question and THX ! – BatOOn Oct 31 '16 at 16:34

1 Answers1

2

here is a link to JSFIDDLE woking code

It should be mapArray[i][x] and I added mapArray[i]=[]; in the outer loop.

here is your fixed code:

var mapSizex=5;
var mapSizey=6;
var mapArray=[];

$(function() {
    console.log( "ready!" );
    $('#map-draw').html(drawMap());
});

function mapGenerator(){
    for(i=0;i<mapSizex;i++){
    mapArray[i]=[];
        for(x=0;x<mapSizey;x++){
            mapArray[i][x]= getRandom(1,5);
      console.log(i,x,getRandom(1,5))
        }
    }
}
function drawMap(){
    mapGenerator();
  console.log(mapArray)
    var map='';
    tileID=0;
    for(i=0;i<mapSizex;i++){
        map=map+'<br style="clear: both;">';
        for(x=0;x<mapSizey;x++){
            map=map+'<div class="tile tileID'+tileID+'">'+mapArray[i][x]+'</div>';
            tileID++;
        }
    }return map;
}
function getRandom(min,max) {
    var x = Math.floor((Math.random() * max) + min);
    return x;
}
O_Z
  • 1,515
  • 9
  • 11