Sorry if I\'ve made mistakes writing this post. I\'m new here and I don\'t know how this works, hope I learn quick. I am also new at JavaScript.
So the question is:
You could also refactor as follows to cut out the if
/switch
:
var commands = {
'switch()': 'Switching background',
'switch(background)': 'Switching background',
'blur()': 'Blurring elements',
'showInfo()': 'Showing info'
};
$(".code").on("click", function () {
var codePrompt = prompt("Set the code in the command line.");
if (commands.hasOwnProperty(codePrompt)) {
alert(commands[codePrompt] + '...');
console.log("Used '" + codePrompt + "' command.");
} else {
alert("Wrong command, try again.");
console.log("Wrong command, try again.");
}
});