1

I've got problems with my variables when i'm doing a function. This is just a silly example. In my code I've got a lot of variables I want to use in the function so I don't have to write function over and over again for each variable "ex1, ex2 etc.." Below here is what I want to do really simple. First check for "ex1" where it equals the declared value, then perform action (animation in real code). Then do the same for "ex2" and so on. Is there a simple way to do this?

<script>
var ex1 = 'frog'; //Those are not set manually. ID's in real code
var ex2 = 'pig';
var ex3 = 'horse';
var ex4 = 'bird';

var x = 0;
setInterval("call", 5000);
function call(){

    x++;

    if(('ex' + x) == 'frog'){
    //action a
    }
    else if(('ex' + x) == 'pig'){
    //action b
    }
    else if(('ex' + x) == 'horse'){
    //action c 
    }
    else if(('ex' + x) == 'bird'){
    //action d
    }

}

</script>
4
  • 1
    window['ex'+x]. There should be a duplicate. Commented Oct 23, 2013 at 8:37
  • like if(window['ex' + x] == y) ? @Zeta Commented Oct 23, 2013 at 8:39
  • I think making ex.. an array will make it easier. Commented Oct 23, 2013 at 8:40
  • Yes, window holds all global variables. Commented Oct 23, 2013 at 8:40

2 Answers 2

2

Global variables are properties of the window object (in a browser anyways). You can access properties using square bracket notation like this:

var ex1 = 'frog'; //Those are not set manually. ID's in real code
var ex2 = 'pig';
var ex3 = 'horse';
var ex4 = 'bird';

var x = 0;

function call(){

    x++;

    if(window['ex' + x] === 'frog'){
    //action a
    }
    else if(window['ex' + x] === 'pig'){
    //action b
    }
    else if(window['ex' + x] === 'horse'){
    //action c 
    }
    else if(window['ex' + x] === 'bird'){
    //action d
    }

}

setInterval(call, 5000);

However, making ex an array would probably be better here:

var ex = [];
ex[1] = 'frog'; //Those are not set manually. ID's in real code
ex[2] = 'pig';
ex[3] = 'horse';
ex[4] = 'bird';

var x = 0;

function call(){

    x++;

    if(ex[x] === 'frog'){
    //action a
    }
    else if(ex[x] === 'pig'){
    //action b
    }
    else if(ex[x] === 'horse'){
    //action c 
    }
    else if(ex[x] === 'bird'){
    //action d
    }

}

setInterval(call, 5000);

If you're doing this for a lot of strings, use a switch statement:

var ex = [];
ex[1] = 'frog'; //Those are not set manually. ID's in real code
ex[2] = 'pig';
ex[3] = 'horse';
ex[4] = 'bird';

var x = 0;

function call(){

    x++;
    switch(ex[x]) {
       case 'frog':
           //action a
           break;
       case 'pig':
           //action b
           break;
       case 'horse':
           //action c
           break;
       case 'bird':
           //action d
           break;
    }

}

setInterval(call, 5000);
Sign up to request clarification or add additional context in comments.

Comments

1

Also, regarding the ifs, a more elegant approach would be to have an object containing all the actions, like this:

var actions = {
  frog:function(){
  //action a
  },
  pig:function(){
    //action b
  }
}

and then just find the action in the object and call it if found

var action = actions['ex' + x]
if (action) {
  action();
}

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.