(function () { "use strict"; // object instead of switch // check for oddf or even number using the function insides the object const options = { odd: (item) => item % 2 === 1, even: (item) => item % 2 === 0, }; const number = 7; const checkValue = "odd"; const checked = options[checkValue](number); // returns true of false if (checked) { console.log(number); } const testArray = [3, 4, 5, 6, 8, 0, 12, 40, 12, 3]; function filterArray(array, position) { return array.filter((item) => options[position](item)); } const getOdd = filterArray(testArray, "odd"); console.log("Odd", getOdd); const getEven = filterArray(testArray, "even"); console.log("Even", getEven); })();
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter