3 ms·
I don't know, in this case, with a bit of reformatting the switch can look pretty similar: function processUserInput(command) { switch (command) {
by Leszek 13y ago
I don't know, in this case, with a bit of reformatting the switch can look pretty similar:
function processUserInput(command) {
switch (command) {
case "north": movePlayer("north"); break;
case "east": movePlayer("east"); break;
case "south": movePlayer("south"); break;
case "west": movePlayer("west"); break;
case "look": describeLocation(); break;
case "backpack": showBackpack(); break;
}
}
Although that's to be expected, a switch on a string value isn't that much different to a string lookup in an associative array.
- Tactic 13y agoExcept with an array you can change things programmatically on the fly. Or have it setup in a configuration file or database table. Very useful.