Fewer lines for switch statement?
conditional-statements, javascript, jquery, switch-statement
Solution
Just put the codes into an array, and then you can simply check if the value is in the array. Since you are using jQuery, you already have an `inArray()` method to do this.
var keycodes = [13, 16, 17, 18, 19]; //and so on
//if the keycode is not found in the array, the result will be -1
if ($.inArray(e.keyCode, keycodes) === -1) {
$(this).addClass(fill);
}
Problem
I wonder if this … ``` inputs.keydown(function (e) { switch (e.keyCode) { case 13: //Enter case 16: //Shift case 17: //Ctrl case 18: //Alt case 19: //Pause/Break case 20: //Caps Lock case 27: //Escape case 35: //End case 36: //Home case 37: //Left case 38: //Up case 39: //Right case 40: //Down // Mac CMD Key case 91: //Safari, Chrome case 93: //Safari, Chrome case 224: //Firefox break; default: $(this).addClass(fill); break; } }); ``` … is also possible with fewer lines? I know I could do an if-condition, but I wonder if I missed something like `case 13 && 16 && …` Maybe some of you know a better practice to check all the cases and write fewer lines of code. I'm just wondering. Thank you in advance!