Since I've started programming in JavaScript fairly recently (around 3-4 weeks ago), I've been getting quite into it. I still keep finding new methods and to do certain tasks, statements, etc. Currently, I'm doing a bit of experiment on it by doing a simple text adventure game.
HTML code:
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.0.0.min.js"></script>
<script src="game.js"></script>
</head>
<body>
<input type="text" maxlength="20"></input>
<button>Enter</button>
</body>
</html>
But I was thinking that instead of adding a switch statement and add each case with its own calling of the function, I wanted to try something out but am unsure how to achieve it.
Instead of this:
//game.js
var storySection = 0;
var user;
$(document).ready(function(){
$('button').click(function(){
user = $('input').val();
switch(storySection){
case 0:
_0();
break;
case 1:
_1();
break;
case 2:
_2();
break;
//And so on...
}
});
});
I wanted to use something like this:
//game.js
var storySection = 0;
var user;
$(document).ready(function(){
$('button').click(function(){
user = $('input').val();
'_' + storySection + ();
});
});
I guess something along those lines, but I was wondering is it possible? Do I have to do some kind of work around it or do I have to forcefully use switch statements...?