Reputation: 5
I'm learning javascript and I want to fill an array in one function and pass it as a parameter in the second function. I have the following code but for some reason it is not working.
function gameplay(tmp) {
for (var i = 0; i < tmp.length; i++) {
if (xTrans >= tmp[0] - 1
&& xTrans < tmp[0]
&& zTrans <= tmp[1] - 1
&& zTrans > tmp[1])
{
//some code here
}
}
}
function fillMap() {
var bounds = new Array();
for (var y = 0; y < map.length; y++) {
for (var x = 0; x < map[0].length; x++) {
if (map[y][x] == '1') {
bounds[length] = 4 * x;
length++;
bounds[length] = -y - 4 * y;
length++;
}
}
}
return bounds;
}
and I call like this:
var tmp = fillMap();
gameplay(tmp);
Thanks in advance..
Upvotes: 0
Views: 77
Reputation: 82058
You might want to try this just to confirm what happens when you have a clear and obvious input:
function fillMap() {
var map = [[0]];
That should give you the output of: [4, -5]
Some potential issues
It seems unlikely, based on what I see, that the if condition can be met:
xTrans >= tmp[0]-1 && xTrans < tmp[0]
// translates to if xTrans == tmp[0] - 1
Some other thoughts
bounds.push
instead of bounds[length] = $val
for(var y=0; y<map.length ;y++) { for(var x=0; x<map[0].length ;x++) {
would be safer/faster as var yLen = map.length; var xLen = map[0].length; for(var y=0; y<yLen ;y++) { for(var x=0; x<xLen ;x++) {
bounds[length] = -y - 4 * y
is the same as bounds[length] = -5 * y;
Upvotes: 1
Reputation: 1
From you comment it seems that array is not being filled as you have some condition to fill array . So first check you condition is being full filled or not.
Upvotes: 0