TheNuttyStudent
TheNuttyStudent

Reputation: 199

Efficient way to if else statement with hundreds of blocks

I've got a list of hundreds of numbers that the user can click on, each number corresponds with a scene variable that ends with the same number the user clicks on, ie if user picked "43", renderer.render(scene43,camera) is performed.

//jumpSwitcher is defined the numerical value that the user has clicked on 
jumpSwitcher = +value;

if (jumpSwitcher == 1) {
     renderer.render(scene1, camera);
} else if (jumpSwitcher == 2) {
     renderer.render(scene2, camera);
} 

This could go on hundreds of times though. How do I write the above code so that I may save myself a lot of work. And how do I efficiently define an array worth of hundreds of these scene variables?

Upvotes: 1

Views: 166

Answers (7)

Anish Gupta
Anish Gupta

Reputation: 101

One approach would be to use eval(), but don't.

You can populate a scenes array using this:

scenes = Array.apply(null, Array(100)).map(function () {return new Scene();});

Alternatively, with ES6,

scenes = Array.from(Array(100), () => new Scene())

Then just use:

renderer.render(scenes[jumpSwitcher-1], camera);

Upvotes: 2

Giovane
Giovane

Reputation: 1451

I suggest using "literal object"

var conditions = {
    "1": ..., // treat condition to 1
    "2": ... // treat condition to 2
    "default": ... // default treatment
}

var treatment = conditions[scene] || conditions.default;

Upvotes: 9

Nina Scholz
Nina Scholz

Reputation: 386746

You could use either an array, if you have contiguous numbers

scenes = [scene0, scene1, ..., sceneN ]
// access
renderer.render(scenes[scene], camera);

or an object for numbers with gaps

scenes = { 10: scene10, 15: scene15 }
// access
renderer.render(scenes[scene], camera);

or a Map

scenes = new Map([[4, scene4], [20, scene20]]);
// access
renderer.render(scenes.get(scene), camera);

The other possibillity is to use the programming style return early, which means, to make a condition and end the function if the condition is met.

function callScene(scene) {
    if (scene === 1) {
        renderer.render(scene1, camera);
        return;
    }
    if (scene === 2) {
        renderer.render(scene2, camera);
        return;
    }
    // ...
}

Upvotes: 2

Cerbrus
Cerbrus

Reputation: 72947

Since the scenes appear to be global variables, you can also use this:

renderer.render(window['scene' + jumpSwitcher], camera);

No eval, single line, variable scene number... It doesn't get any more simple than this.

If the scenes aren't on window, replace it with the proper scope object. (this, for example).

Upvotes: 1

István
István

Reputation: 5127

Just simply write this:

"use strict";
renderer.render(eval('scene' + jumpSwitcher), camera);

And you don't need if or switch.

UPDATE: Strict mode added for safer eval(), docs

Upvotes: -10

Thilo
Thilo

Reputation: 262764

Use an array.

var scenes = [ null, scene1, scene2, scene3 ];
renderer.render(scenes[jumpSwitcher], camera);

Upvotes: 5

Aleksandar Matic
Aleksandar Matic

Reputation: 799

Use the switch statement, example

switch(jumpSwitcher)
{
    case 1: {renderer.render(scene1, camera); break;}
    ...
}

Upvotes: 1

Related Questions