Given the array nums consisting of 2n elements in the form [x1,x2,...,xn,y1,y2,...,yn].

Return the array in the form [x1,y1,x2,y2,...,xn,yn].

Example:

Input: nums = [2,5,1,3,4,7], n = 3
Output: [2,3,5,4,1,7] 
Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7].

Solution

/**
 * @param {number[]} nums
 * @param {number} n
 * @return {number[]}
 */
var shuffle = function(nums, n) {
    //Combine the nums array with n in index order
    return nums.reduce((acc,curr,i) => {
        //Check if i is less than n
        if(i < n){
            //Push the current element and the element at n+i index
            acc.push(curr,nums[n+i]);
        }
        //Return the acc
        return acc;
    },[]);
};