TheNuttyStudent TheNuttyStudent - 2 months ago 10
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?


Just simply write this:

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

And you don't need if or switch.