1

I have a java script and I want to have a function which takes a parameter and execute different function that amount of times.

I have something like that for now:

<body onLoad="funct1(5)">

<script>
    funct1(a){
        var house = [];
        int x = 20;
        int y = 250;
        for(var i= 0; i<a; i++){
            house [i] = funct2(x,y);
            y=y+50;
        }

        for (var j = 0; j<a; j++){
            house[j]();
        }
    }
</script>

<script>
    funct2(x,y){
    //doing something with that 2 variables, drawing a house.
    } 
</script>    

I was trying to put that 2 functions in one script but it also didn't work. Is there is any possible way to execute one function from another and put it into an array to store it and to draw it afterwards?

Mateusz
  • 604
  • 8
  • 20
axeMaltesse
  • 67
  • 10

3 Answers3

2

If you do it in one script tag or two, it doesn't matter as anyway the browser will execute them synchronously. Your variable declarations for x and y are wrong. Your function declarations are also wrong. Anyway they are just syntax errors which you shall eventually avoid.

You cannot call "house[j] ()" as you have already executed the function in this statement: "funct2(x,y)". If you want to store the function calls with the parameters assigned before hand, then you should use : "house [i] = funct2.bind(this, x, y)" i.e. you are binding the parameters to the function whenever it will be called and storing the copy of this callable binded function in house[i].

I suppose this is what you want:

<body onLoad="funct1(5)">

<script>
    function funct1(a){
        var house = [];
        var x = 20;
        var y = 250;
        for(var i= 0; i<a; i++){
            house [i] = funct2.bind(this, x, y);
            y=y+50;
        }

        for (var j = 0; j<a; j++){
            house[j]();
        }
    }
</script>

<script>
    function funct2(x,y){
        console.log('drawing house with x '+x+' and y '+y);
    }
</script>
  • Sorry, for slow comment, but previous one did not load. Thank you for your corrections, now it is working perfectly. – axeMaltesse Mar 09 '16 at 11:08
1

This should work from the same script tag. You need to change the int x and int y to var x and var y though:

<script>
    var funct1= function(a){
        var house = [];
        var x = 20;
        var y = 250;
        for(var i= 0; i<a; i++){
            house [i] = funct2(x,y);
            y=y+50;
        }

        for (var j = 0; j<a; j++){
            house[j]();
        }
    }
    var funct2 = function(x,y){
    //doing something with that 2 variables, drawing a house.
        return function() { 
            //do stuff with x and y
        }
    } 
</script>

funct2 now returns a function as well, allowing you to call house[j]() on the result.

millerbr
  • 2,951
  • 1
  • 14
  • 25
0

The thing you are looking for is actually quite complicated. You want func2 to return functions based on x and y. Thats possible by defining them dynamically like this and then return them and save them in the house array.

But why would you want to do this? Don't save them in the house array and just make func2 paint them directly.

Community
  • 1
  • 1
b-pos465
  • 131
  • 4