# Functions, Parameters & Conditions

## Recommended Posts

Hello, I'm needing to write code that do the following.
Take two values: [x, y];
And using conditions change within functions.
The following is:
var x = 0;var y = 0;var b = 2;var c = 3;So;first function of x = valueXOffset (xFunctionFirst) // OffsetIf b = 1return{96 // 5% of 1920}y = 54 // 5% of 1080Else if b = 2return{192 // 10% of 1920}y = 108 // 10% of 1080Else if b = 3return{288 // 15% of 1920}y = 162 // 15% of 1080Else b = (any value)return{0}y = 0second function of x = valueXFinal (xFunctionSecond)If c = 1return{xFunctionFirst}Else If c = 2return{1920/2}Else If c = 3return{1920 - xFunctionFirst}Else c = (any value)0call function here, to get x value and y too.[x, y];
By "y" get stuck within a function, have to follow the conditions, it must be called in the last function.
I think you need to use two functions, something like this example:
the function (x) {// <- function
function b (y) {// <- inner function
return x + y; // <- Use variables from outer scope
}
}
to (3) (4);
And each function use two parameters to represent x and y.
As a way to array.
In the function of "x", if you need to call a value, which is 1920.
And in function of "y" 1080.
What this code does is every time I increase 1-3, b, x rise or fall by 5%, and y will increase by 5% from 1 to 1.
And 1 to 3 passes c 0 + offset, 960 = half, and then in 1920, the total - offset.

The restrictions I have, it is to be done with functions, please.

Edited by tiscavalcanti
##### Share on other sites

I'm not sure what your question is. Did you post a conversation between you and someone else or something?

##### Share on other sites

I'm not sure what your question is. Did you post a conversation between you and someone else or something?

I myself will continue trying to do. That is it.

##### Share on other sites

I'm not sure what your question is. Did you post a conversation between you and someone else or something?

This is it. I'm able, finally. Thanks for your time!

w=1920;h=1080;p = [".95", ".90", ".85",".05", ".10",".15"];x=0;y=0;b=3;c=3;function xyFinal(){    function variable(x,y){        if(b==1){            return [x*p[3],y*p[0]];            }else if(b==2){                return [x*p[4],y*p[1]];                }else if(b==3){                    return [x*p[5],y*p[2]];                    }                }                var o=variable(w,h);            if(c==1){                return o;                }else if(c==2){                    return [w/2,o[1]];                    }else if(c==3){                        return [w-o[0],o[1]];                        }                    }                    v=xyFinal();                x=v[0];                y=v[1];[x,y];
Edited by tiscavalcanti
##### Share on other sites

That's not a question, that's code.

Is the code doing what you want it to? If it isn't, then what is it actually doing and what did you want it to do?

Since you're treating the elements of array p like numbers, you shouldn't put them in quotation marks:

p = [.95, .90, .85, .05, .10, .15];

Be sure to declare all your variables with the var keyword like this

var w = 1920;var h = 1080;

The indentation in your code is confusing. The else if statement should be nested at the same level as the if that preceded it. This also looks like a good place for a switch statement instead of all those if conditions.

switch( {  case 1: return [x*p[3],y*p[0]];  case 2: return [x*p[4],y*p[1]];  case 3: return [x*p[5],y*p[2]];}

I've changed the indentation of your current code to make it more readable:

function xyFinal(){  function variable(x,y){    if(b==1){      return [x*p[3],y*p[0]];    }else if(b==2){      return [x*p[4],y*p[1]];    }else if(b==3){      return [x*p[5],y*p[2]];    }  }  var o = variable(w,h);  if(c==1){    return o;  }else if(c==2){    return [w/2,o[1]];  }else if(c==3){    return [w-o[0],o[1]];  }}    v=xyFinal();x=v[0];y=v[1];[x,y];

That [x,y] at the end of your code isn't really doing anything because you're not returning it or printing it.

• 1
##### Share on other sites

That's not a question, that's code.

Is the code doing what you want it to? If it isn't, then what is it actually doing and what did you want it to do?

Since you're treating the elements of array p like numbers, you shouldn't put them in quotation marks:

p = [.95, .90, .85, .05, .10, .15];

Be sure to declare all your variables with the var keyword like this

var w = 1920;var h = 1080;

The indentation in your code is confusing. The else if statement should be nested at the same level as the if that preceded it. This also looks like a good place for a switch statement instead of all those if conditions.

switch( {  case 1: return [x*p[3],y*p[0]];  case 2: return [x*p[4],y*p[1]];  case 3: return [x*p[5],y*p[2]];}

I've changed the indentation of your current code to make it more readable:

function xyFinal(){  function variable(x,y){    if(b==1){      return [x*p[3],y*p[0]];    }else if(b==2){      return [x*p[4],y*p[1]];    }else if(b==3){      return [x*p[5],y*p[2]];    }  }  var o = variable(w,h);  if(c==1){    return o;  }else if(c==2){    return [w/2,o[1]];  }else if(c==3){    return [w-o[0],o[1]];  }}    v=xyFinal();x=v[0];y=v[1];[x,y];

That [x,y] at the end of your code isn't really doing anything because you're not returning it or printing it.

Oh, yeah! Thank you very much, help me a lot! This code is my result after many tries, and finally I got it. And with your instructions, will stay very best.

Edited by tiscavalcanti
##### Share on other sites

How to use a "case" statement with two "conditons"? For example:

switch(b) {"case 1 or 4:" }

Thanks.

Edited by tiscavalcanti
##### Share on other sites

Like this:

switch( {  case 1:  case 4:    // Do something  break;}
• 1
##### Share on other sites

Like this:

switch( {  case 1:  case 4:    // Do something  break;}

Ok. Good. I needed use a Array in final of code, because After Effects require it.

My code was practically perfect. I'm going to change only a few details.
Thank you very much!
Edited by tiscavalcanti