Solving Popular Algorithms: Shuffle the Array

A good boy doing a good shuffle
nums = [2,5,1,3,4,7], n = 3
[2,3,5,4,1,7] 
let partOne = []for (let i = 0; i < nums.length/2; i++) {
partOne.push(nums[i])
}
let partTwo = []for (let j= n; j < nums.length; j++) {
partTwo.push(nums[j])
}
let finalArr = []while (partOne.length > 0 && partTwo.length > 0) {
finalArr.push(partOne.shift(), partTwo.shift()
}
shuffle = (nums, n) => {
let partOne = []
for (let i = 0; i < nums.length/2; i++) {
partOne.push(nums[i])
}
let partTwo = []
for (let j= n; j < nums.length; j++) {
partTwo.push(nums[j])
}
let finalArr = []
while (partOne.length > 0 && partTwo.length > 0) {
finalArr.push(partOne.shift(), partTwo.shift()
}
return finalArr
}
Happy dancing for two reasons: 1) I solved this problem and 2) I won another game in my fantasy league!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store