We are required to write a JavaScript function that takes in an array of numbers and returns an array with all even numbers appearing on the left side of any odd number and all the odd numbers appearing on the right side of any even number.
Therefore, let's write the code for this function −
Example
const arr = [2, 6, 3, 7, 8, 3, 5, 4, 3, 6, 87, 23, 2, 23, 67, 4]; const isEven = num => num % 2 === 0; const sorter = (a, b) => { if(isEven(a) && !isEven(b)){ return -1; }; if(!isEven(a) && isEven(b)){ return 1; }; return 0; }; arr.sort(sorter); console.log(arr);
Output
The output in the console will be −
[ 2, 6, 8, 4, 6, 2, 4, 3, 7, 3, 5, 3, 87, 23, 23, 67 ]