0

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..

6
  • Are you getting some kind of error message, or is the output/result simply not what you expect? Commented May 15, 2014 at 17:12
  • Make sure your code runs at all. The length variable is not defined. I assume you want bounds.push(...) instead. If you are new to arrays in JS, I recommend to a tutorial: developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/… . Commented May 15, 2014 at 17:13
  • length is defined and the message i get when im trying to print the array is that it is empty. Commented May 15, 2014 at 17:17
  • Where is map defined? Commented May 15, 2014 at 17:19
  • In for(var y=0; y<map.length ;y++), map ss global variable? Is It initiate with what value? Commented May 15, 2014 at 17:20

2 Answers 2

1

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

  • map isn't defined anywhere
  • length isn't defined anywhere
  • xTrans is not defined anywhere
  • zTrans is not defined anywhere
  • 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

  • You're better off using 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;
Sign up to request clarification or add additional context in comments.

1 Comment

I tried push and the for statement as you said and the other variables are defined but still the same problem
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.