PauloNunes
PauloNunes

Reputation: 9

Can I implement/put an array on a switch conditional?

I was building my code when came to my mind a bizarre idea, can I implement/put an array inside a switch?

I mean, how can I make the codeHide case work? with this piece of code it don't work.

When I ask to set the command and I put hide() (that is codeHide[0] on the codeHide array) I want to switch take the codeHide case (my if-statement) and return an alert telling me the alertMessage of that particular array element.

If I put hide(background) (that is codeHide[1] on the codeHide array) I want to switch take the codeHide case else (of my if-statement) and return an alert telling me the alertMessage of that particular array element(in the is-statement).

Hope you understand me.

Doing this it don't work and I think it's because the "case codeHide:".

And this is what I've done so far:

var codeHide = ['hide()', 'hide(background)'];

$(".code").on("click", function () {
    var codePrompt = prompt("Set the code in the command line."),
    alertMessage = "",
    consoleMessage = "Used '" + codePrompt + "' command.";

switch (codePrompt) {
    case codeHide:
        if (codeHide[0]) {
            alertMessage = "Hiding elements...";
        } else {
            alertMessage = "Hiding Background...";
        }
        break;
    default:
        alertMessage = consoleMessage = "We are sorry but you entered a WRONG command, try again tho!\ntyped: " + codePrompt;
        break;
    }
    alert(alertMessage);
    console.log(consoleMessage);
});

Upvotes: 0

Views: 119

Answers (2)

Oriol
Oriol

Reputation: 288500

I think you are trying something like

var commands = {
    hide: 'hide()',
    hideBg: 'hide(background)'
};

var codePrompt = prompt("Set the code in the command line."),
    alertMessage;

switch (codePrompt) {
    case commands.hide:
        alertMessage = "Hiding elements...";
        break;
    case commands.hideBg:
        alertMessage = "Hiding Background...";
        break;
    default:
        alertMessage = "WRONG command";
        break;
    }
}

However, you can also use

var commands = {
    'hide()': "Hiding elements...",
    'hide(background)': "Hiding Background..."
};

var codePrompt = prompt("Set the code in the command line.");

var alertMessage = commands[codePrompt] || "WRONG command";

I guess you also want to run some functions:

var commands = {
    'hide()': {
        text: "Hiding elements...",
        funcion: someFunctionToHide
    },
    'hide(background)': {
        text: "Hiding Background...",
        funcion: someFunctionToHideBackground
    }
};

var codePrompt = prompt("Set the code in the command line."),
    command = commands[codePrompt];

if(!command) {
    alertMessage = "WRONG command";
} else {
    alertMessage = command.text;
    command.function();
}

Upvotes: 2

Jon
Jon

Reputation: 437554

switch operates by comparing the value being switched on to each of the possible cases using the identity operator ===. This means that you can put an array inside a case, and it will work as specified (but certainly not very intuitively for arrays):

var x = [1];
var a = [1];

switch (x) {
    case [1]: alert("it's [1]!"); break;
    case a: alert("it's a!"); break;
    case x: alert("it's x!"); break;
}

This will alert "it's x!", while you might be expecting that either of the preceding two cases would be "good enough" to trigger. But that's just how === works:

[1] === x   // false
a === x     // true
x === x     // true

So while you can technically use an array, in practice it would be very unusual to have a situation where it's actually useful to do so.

Going back to your code, since the values you are interested in are strings it seems that using a simple object as a map would do just fine:

var commands = {
    "hide()": {
        alert: "Hiding elements...",
        console: "Blah blah"
    }.
    "hide(background)": {
        alert: "Hiding background...",
        console: "Blah blah"
    }.
};
var fallback = {
    alert: "Sorry, wrong command",
    console: "Sorry, wrong command"
};

which would then allow you to write

var result = commands[input] || fallback;
alert(result.alert);
console.log(result.console);

Upvotes: 1

Related Questions