TheNuttyStudent TheNuttyStudent - 1 year ago 67
Javascript Question

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?

Answer Source

Just simply write this:

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

And you don't need if or switch.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download